Skip to content

Commit 43705d7

Browse files
fix: merge Engine.IO options
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 }); ```
1 parent 118cc68 commit 43705d7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ export class Server extends EventEmitter {
169169
> = new Map();
170170
private _adapter: any;
171171
private _serveClient: boolean;
172+
private opts: Partial<EngineOptions>;
172173
private eio;
173174
private engine;
174175
private _path: string;
@@ -203,7 +204,8 @@ export class Server extends EventEmitter {
203204
this.encoder = new this._parser.Encoder();
204205
this.adapter(opts.adapter || Adapter);
205206
this.sockets = this.of("/");
206-
if (srv) this.attach(srv, opts);
207+
this.opts = opts;
208+
if (srv) this.attach(srv);
207209
}
208210

209211
/**
@@ -357,6 +359,8 @@ export class Server extends EventEmitter {
357359
srv.listen(port);
358360
}
359361

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

test/socket.io.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,24 @@ describe("socket.io", () => {
138138
done();
139139
});
140140
});
141+
142+
it("should work with #attach (and merge options)", () => {
143+
const srv = createServer((req, res) => {
144+
res.writeHead(404);
145+
res.end();
146+
});
147+
const server = new Server({
148+
pingTimeout: 6000
149+
});
150+
server.attach(srv, {
151+
pingInterval: 24000
152+
});
153+
// @ts-ignore
154+
expect(server.eio.opts.pingTimeout).to.eql(6000);
155+
// @ts-ignore
156+
expect(server.eio.opts.pingInterval).to.eql(24000);
157+
server.close();
158+
});
141159
});
142160

143161
describe("port", () => {

0 commit comments

Comments
 (0)