From fbea5c97ccd248d643afe578cc488145cd5b0a65 Mon Sep 17 00:00:00 2001 From: Billouboq Date: Sat, 3 Dec 2016 11:02:31 +0100 Subject: [PATCH 1/5] optimisation --- .gitignore | 1 + lib/transports/websocket.js | 58 +++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index cb265cefb..d36e40b28 100755 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ npm-debug.log coverage.html lib-cov/ dist +.idea \ No newline at end of file diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 330fd4e38..706e83b97 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -28,11 +28,13 @@ function WebSocket (req) { this.socket.on('message', this.onData.bind(this)); this.socket.once('close', this.onClose.bind(this)); this.socket.on('error', this.onError.bind(this)); - this.socket.on('headers', function (headers) { - self.emit('headers', headers); - }); + this.socket.on('headers', onHeaders); this.writable = true; this.perMessageDeflate = null; + + function onHeaders (headers) { + self.emit('headers', headers); + } } /** @@ -86,31 +88,37 @@ WebSocket.prototype.onData = function (data) { WebSocket.prototype.send = function (packets) { var self = this; - packets.forEach(function (packet) { - parser.encodePacket(packet, self.supportsBinary, function (data) { - debug('writing "%s"', data); - - // always creates a new object since ws modifies it - var opts = {}; - if (packet.options) { - opts.compress = packet.options.compress; - } - if (self.perMessageDeflate) { - var len = 'string' === typeof data ? Buffer.byteLength(data) : data.length; - if (len < self.perMessageDeflate.threshold) { - opts.compress = false; - } + for (var i = 0; i < packets.length; i++) { + var packet = packets[i]; + parser.encodePacket(packet, self.supportsBinary, parserCb); + } + + function parserCb (data) { + debug('writing "%s"', data); + + // always creates a new object since ws modifies it + var opts = {}; + if (packet.options) { + opts.compress = packet.options.compress; + } + + if (self.perMessageDeflate) { + var len = 'string' === typeof data ? Buffer.byteLength(data) : data.length; + if (len < self.perMessageDeflate.threshold) { + opts.compress = false; } + } + + self.writable = false; + self.socket.send(data, opts, sendCb); + } - self.writable = false; - self.socket.send(data, opts, function (err) { - if (err) return self.onError('write error', err.stack); - self.writable = true; - self.emit('drain'); - }); - }); - }); + function sendCb (err) { + if (err) return self.onError('write error', err.stack); + self.writable = true; + self.emit('drain'); + } }; /** From 93c390b1cd99358bdf290d57a49fa3797f05d75b Mon Sep 17 00:00:00 2001 From: Billouboq Date: Sat, 3 Dec 2016 11:04:22 +0100 Subject: [PATCH 2/5] end of line --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d36e40b28..b534d3567 100755 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ npm-debug.log coverage.html lib-cov/ dist -.idea \ No newline at end of file +.idea From 7a712c5c6dd2460c8527fec050fd8b2d9d92f3e8 Mon Sep 17 00:00:00 2001 From: Billouboq Date: Mon, 5 Dec 2016 22:20:53 +0100 Subject: [PATCH 3/5] change functions name as suggested --- lib/transports/websocket.js | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 706e83b97..d31c2ce8a 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -91,10 +91,10 @@ WebSocket.prototype.send = function (packets) { for (var i = 0; i < packets.length; i++) { var packet = packets[i]; - parser.encodePacket(packet, self.supportsBinary, parserCb); + parser.encodePacket(packet, self.supportsBinary, send); } - function parserCb (data) { + function send (data) { debug('writing "%s"', data); // always creates a new object since ws modifies it @@ -111,10 +111,10 @@ WebSocket.prototype.send = function (packets) { } self.writable = false; - self.socket.send(data, opts, sendCb); + self.socket.send(data, opts, onEnd); } - function sendCb (err) { + function onEnd (err) { if (err) return self.onError('write error', err.stack); self.writable = true; self.emit('drain'); diff --git a/package.json b/package.json index 92630a0da..6be8e8276 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "mocha": "2.3.4", "s": "0.1.1", "superagent": "0.15.4", - "uws": "0.4.0" + "uws": "0.12.0" }, "scripts": { "test": "gulp test; EIO_WS_ENGINE=uws gulp test;" From 2183d550b57d36ea2f42d4f738ce4cf9c994198d Mon Sep 17 00:00:00 2001 From: Billouboq Date: Mon, 5 Dec 2016 22:21:58 +0100 Subject: [PATCH 4/5] fix dependency bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6be8e8276..92630a0da 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "mocha": "2.3.4", "s": "0.1.1", "superagent": "0.15.4", - "uws": "0.12.0" + "uws": "0.4.0" }, "scripts": { "test": "gulp test; EIO_WS_ENGINE=uws gulp test;" From 341d1675d5155156019b287c9580a6a5339a34d7 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 5 Dec 2016 23:43:30 +0100 Subject: [PATCH 5/5] remove .idea from .gitignore That should be in your global .gitignore, shouldn't it? --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index b534d3567..cb265cefb 100755 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ npm-debug.log coverage.html lib-cov/ dist -.idea