Skip to content

Commit

Permalink
fix: merge Engine.IO options
Browse files Browse the repository at this point in the history
So that the following example:

```js
const io = require('socket.io')({
  pingTimeout: 10000
});

io.listen(3000);
```

behaves the same as:

```js
const io = require('socket.io')(3000, {
  pingTimeout: 10000
});
```

Before this change, the options in the first example were not forwarded
to the Engine.IO constructor, which is not really intuitive.

The previous syntax (which is still valid):

```js
const io = require('socket.io')();

io.listen(3000, {
  pingTimeout: 10000
});
```
  • Loading branch information
darrachequesne committed Nov 17, 2020
1 parent 118cc68 commit 43705d7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export class Server extends EventEmitter {
> = new Map();
private _adapter: any;
private _serveClient: boolean;
private opts: Partial<EngineOptions>;
private eio;
private engine;
private _path: string;
Expand Down Expand Up @@ -203,7 +204,8 @@ export class Server extends EventEmitter {
this.encoder = new this._parser.Encoder();
this.adapter(opts.adapter || Adapter);
this.sockets = this.of("/");
if (srv) this.attach(srv, opts);
this.opts = opts;
if (srv) this.attach(srv);
}

/**
Expand Down Expand Up @@ -357,6 +359,8 @@ export class Server extends EventEmitter {
srv.listen(port);
}

// merge the options passed to the Socket.IO server
Object.assign(opts, this.opts);
// set engine.io path to `/socket.io`
opts.path = opts.path || this._path;

Expand Down
18 changes: 18 additions & 0 deletions test/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,24 @@ describe("socket.io", () => {
done();
});
});

it("should work with #attach (and merge options)", () => {
const srv = createServer((req, res) => {
res.writeHead(404);
res.end();
});
const server = new Server({
pingTimeout: 6000
});
server.attach(srv, {
pingInterval: 24000
});
// @ts-ignore
expect(server.eio.opts.pingTimeout).to.eql(6000);
// @ts-ignore
expect(server.eio.opts.pingInterval).to.eql(24000);
server.close();
});
});

describe("port", () => {
Expand Down

0 comments on commit 43705d7

Please sign in to comment.