From a350b739688ecc3716cfb3c771c316544901cb58 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 18 Jan 2017 12:58:52 +0100 Subject: [PATCH 1/3] Add an `additionalHandshakePacket` option --- README.md | 1 + lib/server.js | 1 + lib/socket.js | 4 ++++ test/server.js | 14 +++++++++++++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c744f395e..1fbf8d490 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ to a single process. Set false to not save io cookie on all requests. (`/`) - `cookieHttpOnly` (`Boolean`): If `true` HttpOnly io cookie cannot be accessed by client-side APIs, such as JavaScript. (`true`) _This option has no effect if `cookie` or `cookiePath` is set to `false`._ - `wsEngine` (`String`): what WebSocket server implementation to use. Specified module must conform to the `ws` interface (see [ws module api docs](https://github.com/websockets/ws/blob/master/doc/ws.md)). Default value is `ws`. An alternative c++ addon is also available by installing `uws` module. + - `additionalHandshakePacket` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO. - `close` - Closes all clients - **Returns** `Server` for chaining diff --git a/lib/server.js b/lib/server.js index a5bca7dba..9370e71bd 100644 --- a/lib/server.js +++ b/lib/server.js @@ -49,6 +49,7 @@ function Server (opts) { this.cookieHttpOnly = false !== opts.cookieHttpOnly; this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || true) : false; this.httpCompression = false !== opts.httpCompression ? (opts.httpCompression || {}) : false; + this.additionalHandshakePacket = opts.additionalHandshakePacket; var self = this; diff --git a/lib/socket.js b/lib/socket.js index 425008908..5c0a47043 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -65,6 +65,10 @@ Socket.prototype.onOpen = function () { pingTimeout: this.server.pingTimeout })); + if (this.server.additionalHandshakePacket) { + this.sendPacket('message', this.server.additionalHandshakePacket); + } + this.emit('open'); this.setPingTimeout(); }; diff --git a/test/server.js b/test/server.js index 8c49b3ddc..06fc1b231 100644 --- a/test/server.js +++ b/test/server.js @@ -397,6 +397,18 @@ describe('server', function () { }); }); }); + + it('should send a packet along with the handshake', function (done) { + listen({ additionalHandshakePacket: 'faster!' }, function (port) { + var socket = new eioc.Socket('ws://localhost:%d'.s(port)); + socket.on('open', function () { + socket.on('message', function (msg) { + expect(msg).to.be('faster!'); + done(); + }); + }); + }); + }); }); describe('close', function () { @@ -485,7 +497,7 @@ describe('server', function () { }); it('should trigger on both ends upon ping timeout', function (done) { - var opts = { allowUpgrades: false, pingTimeout: 500, pingInterval: 10 }; + var opts = { allowUpgrades: false, pingTimeout: 50, pingInterval: 50 }; var engine = listen(opts, function (port) { var socket = new eioc.Socket('ws://localhost:%d'.s(port)); var total = 2; From af3bebf36a44ec2f91e5eb87475d231682580215 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 19 Jan 2017 14:37:34 +0100 Subject: [PATCH 2/3] use initialMessage instead of additionalHandshakePacket --- README.md | 2 +- lib/server.js | 2 +- lib/socket.js | 4 ++-- test/server.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1fbf8d490..12b78edef 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ to a single process. Set false to not save io cookie on all requests. (`/`) - `cookieHttpOnly` (`Boolean`): If `true` HttpOnly io cookie cannot be accessed by client-side APIs, such as JavaScript. (`true`) _This option has no effect if `cookie` or `cookiePath` is set to `false`._ - `wsEngine` (`String`): what WebSocket server implementation to use. Specified module must conform to the `ws` interface (see [ws module api docs](https://github.com/websockets/ws/blob/master/doc/ws.md)). Default value is `ws`. An alternative c++ addon is also available by installing `uws` module. - - `additionalHandshakePacket` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO. + - `initialMessage` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO. - `close` - Closes all clients - **Returns** `Server` for chaining diff --git a/lib/server.js b/lib/server.js index 9370e71bd..551c86041 100644 --- a/lib/server.js +++ b/lib/server.js @@ -49,7 +49,7 @@ function Server (opts) { this.cookieHttpOnly = false !== opts.cookieHttpOnly; this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || true) : false; this.httpCompression = false !== opts.httpCompression ? (opts.httpCompression || {}) : false; - this.additionalHandshakePacket = opts.additionalHandshakePacket; + this.initialMessage = opts.initialMessage; var self = this; diff --git a/lib/socket.js b/lib/socket.js index 5c0a47043..2038d786f 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -65,8 +65,8 @@ Socket.prototype.onOpen = function () { pingTimeout: this.server.pingTimeout })); - if (this.server.additionalHandshakePacket) { - this.sendPacket('message', this.server.additionalHandshakePacket); + if (this.server.initialMessage) { + this.sendPacket('message', this.server.initialMessage); } this.emit('open'); diff --git a/test/server.js b/test/server.js index 06fc1b231..78bf0beef 100644 --- a/test/server.js +++ b/test/server.js @@ -399,7 +399,7 @@ describe('server', function () { }); it('should send a packet along with the handshake', function (done) { - listen({ additionalHandshakePacket: 'faster!' }, function (port) { + listen({ initialMessage: 'faster!' }, function (port) { var socket = new eioc.Socket('ws://localhost:%d'.s(port)); socket.on('open', function () { socket.on('message', function (msg) { From 37ab5f379868c40f11ac4b33b1c1a0304a04b758 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Sun, 22 Jan 2017 07:46:49 +0100 Subject: [PATCH 3/3] initialPacket sounds better --- README.md | 2 +- lib/server.js | 2 +- lib/socket.js | 4 ++-- test/server.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 12b78edef..0bdb44770 100644 --- a/README.md +++ b/README.md @@ -227,7 +227,7 @@ to a single process. Set false to not save io cookie on all requests. (`/`) - `cookieHttpOnly` (`Boolean`): If `true` HttpOnly io cookie cannot be accessed by client-side APIs, such as JavaScript. (`true`) _This option has no effect if `cookie` or `cookiePath` is set to `false`._ - `wsEngine` (`String`): what WebSocket server implementation to use. Specified module must conform to the `ws` interface (see [ws module api docs](https://github.com/websockets/ws/blob/master/doc/ws.md)). Default value is `ws`. An alternative c++ addon is also available by installing `uws` module. - - `initialMessage` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO. + - `initialPacket` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO. - `close` - Closes all clients - **Returns** `Server` for chaining diff --git a/lib/server.js b/lib/server.js index 551c86041..0da4c234c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -49,7 +49,7 @@ function Server (opts) { this.cookieHttpOnly = false !== opts.cookieHttpOnly; this.perMessageDeflate = false !== opts.perMessageDeflate ? (opts.perMessageDeflate || true) : false; this.httpCompression = false !== opts.httpCompression ? (opts.httpCompression || {}) : false; - this.initialMessage = opts.initialMessage; + this.initialPacket = opts.initialPacket; var self = this; diff --git a/lib/socket.js b/lib/socket.js index 2038d786f..f77a4ca07 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -65,8 +65,8 @@ Socket.prototype.onOpen = function () { pingTimeout: this.server.pingTimeout })); - if (this.server.initialMessage) { - this.sendPacket('message', this.server.initialMessage); + if (this.server.initialPacket) { + this.sendPacket('message', this.server.initialPacket); } this.emit('open'); diff --git a/test/server.js b/test/server.js index 78bf0beef..8e2d8943c 100644 --- a/test/server.js +++ b/test/server.js @@ -399,7 +399,7 @@ describe('server', function () { }); it('should send a packet along with the handshake', function (done) { - listen({ initialMessage: 'faster!' }, function (port) { + listen({ initialPacket: 'faster!' }, function (port) { var socket = new eioc.Socket('ws://localhost:%d'.s(port)); socket.on('open', function () { socket.on('message', function (msg) {