diff --git a/README.md b/README.md index c744f395e..0bdb44770 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. + - `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 a5bca7dba..0da4c234c 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.initialPacket = opts.initialPacket; var self = this; diff --git a/lib/socket.js b/lib/socket.js index 425008908..f77a4ca07 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -65,6 +65,10 @@ Socket.prototype.onOpen = function () { pingTimeout: this.server.pingTimeout })); + if (this.server.initialPacket) { + this.sendPacket('message', this.server.initialPacket); + } + this.emit('open'); this.setPingTimeout(); }; diff --git a/test/server.js b/test/server.js index 8c49b3ddc..8e2d8943c 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({ initialPacket: '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;