Skip to content

Commit

Permalink
[feature] Add an initialPacket option (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 22, 2017
1 parent a3496ed commit 274efa1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/server.js
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions lib/socket.js
Expand Up @@ -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();
};
Expand Down
14 changes: 13 additions & 1 deletion test/server.js
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 274efa1

Please sign in to comment.