Created
November 7, 2017 09:23
-
-
Save wision/90afc18d2e728c4d2c7f5701a251831b to your computer and use it in GitHub Desktop.
binaryjs using uws module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/client.js b/lib/client.js | |
index 921bbcb..17f0f2d 100644 | |
--- a/lib/client.js | |
+++ b/lib/client.js | |
@@ -2,7 +2,7 @@ | |
var Stream = require('stream').Stream; | |
var EventEmitter = require('events').EventEmitter; | |
var BufferReadStream = require('streamers').BufferReadStream; | |
-var WebSocket = require('ws'); | |
+var WebSocket = require('uws'); | |
var util = require('./util'); | |
var BinaryStream = require('./stream').BinaryStream; | |
@@ -33,9 +33,9 @@ function BinaryClient(socket, options) { | |
this._socket.binaryType = 'nodebuffer'; | |
- this._socket.addEventListener('open', function(){ | |
+ this._socket.internalOnOpen = function(){ | |
self.emit('open'); | |
- }); | |
+ }; | |
// if node | |
this._socket.on('drain', function(){ | |
var ids = Object.keys(self.streams); | |
@@ -44,21 +44,23 @@ function BinaryClient(socket, options) { | |
} | |
}); | |
// end node | |
- this._socket.addEventListener('error', function(error){ | |
+ this._socket.internalOnError = function(error){ | |
var ids = Object.keys(self.streams); | |
for (var i = 0, ii = ids.length; i < ii; i++) { | |
self.streams[ids[i]]._onError(error); | |
} | |
self.emit('error', error); | |
- }); | |
- this._socket.addEventListener('close', function(code, message){ | |
+ }; | |
+ this._socket.internalOnClose = function(code, message){ | |
var ids = Object.keys(self.streams); | |
for (var i = 0, ii = ids.length; i < ii; i++) { | |
self.streams[ids[i]]._onClose(); | |
} | |
self.emit('close', code, message); | |
- }); | |
- this._socket.addEventListener('message', function(data, flags){ | |
+ }; | |
+ this._socket.internalOnMessage = function(raw, flags){ | |
+ // first convert arrayBuffer to buffer and then create new copy | |
+ let data = Buffer.from(Buffer.from(raw)); | |
util.setZeroTimeout(function(){ | |
// Message format | |
@@ -92,19 +94,21 @@ function BinaryClient(socket, options) { | |
// [ 6 , null , streamId ] | |
// | |
- data = data.data; | |
try { | |
data = util.unpack(data); | |
} catch (ex) { | |
return self.emit('error', new Error('Received unparsable message: ' + ex)); | |
} | |
- if (!(data instanceof Array)) | |
+ if (!(data instanceof Array)) { | |
return self.emit('error', new Error('Received non-array message')); | |
- if (data.length != 3) | |
+ } | |
+ if (data.length != 3) { | |
return self.emit('error', new Error('Received message with wrong part count: ' + data.length)); | |
- if ('number' != typeof data[0]) | |
+ } | |
+ if ('number' != typeof data[0]) { | |
return self.emit('error', new Error('Received message with non-number type: ' + data[0])); | |
+ } | |
switch(data[0]) { | |
case 0: | |
@@ -166,7 +170,7 @@ function BinaryClient(socket, options) { | |
self.emit('error', new Error('Unrecognized message type received: ' + data[0])); | |
} | |
}); | |
- }); | |
+ }; | |
} | |
util.inherits(BinaryClient, EventEmitter); | |
diff --git a/lib/server.js b/lib/server.js | |
index 543f58b..48071c1 100644 | |
--- a/lib/server.js | |
+++ b/lib/server.js | |
@@ -1,4 +1,4 @@ | |
-var WebSocket = require('ws'); | |
+var WebSocket = require('uws'); | |
var EventEmitter = require('events').EventEmitter; | |
var util = require('./util'); | |
diff --git a/lib/stream.js b/lib/stream.js | |
index 427663e..069d2c6 100644 | |
--- a/lib/stream.js | |
+++ b/lib/stream.js | |
@@ -1,6 +1,7 @@ | |
// if node | |
var Stream = require('stream').Stream; | |
var util = require('./util'); | |
+var WebSocket = require('uws'); | |
// end node | |
function BinaryStream(socket, id, create, meta) { | |
@@ -69,7 +70,7 @@ BinaryStream.prototype._onResume = function() { | |
}; | |
BinaryStream.prototype._write = function(code, data, bonus) { | |
- if (this._socket.readyState !== this._socket.constructor.OPEN) { | |
+ if (this._socket.readyState !== WebSocket.OPEN) { | |
return false; | |
} | |
var message = util.pack([code, data, bonus]); | |
diff --git a/package.json b/package.json | |
index e5b0f0a..03a3508 100644 | |
--- a/package.json | |
+++ b/package.json | |
@@ -18,7 +18,7 @@ | |
"dependencies": { | |
"binarypack": ">=0.0.4", | |
"streamers": ">=0.1.0", | |
- "ws": "^2.2.0" | |
+ "uws": "^8.14.1" | |
}, | |
"devDependencies": { | |
"mocha": "~1.3.0", |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment