diff --git a/lib/index.ts b/lib/index.ts index 069d5e2cbb..063e1a07e7 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -169,6 +169,7 @@ export class Server extends EventEmitter { > = new Map(); private _adapter: any; private _serveClient: boolean; + private opts: Partial; private eio; private engine; private _path: string; @@ -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); } /** @@ -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; diff --git a/test/socket.io.ts b/test/socket.io.ts index c6a44b2f96..0596b5b9a2 100644 --- a/test/socket.io.ts +++ b/test/socket.io.ts @@ -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", () => {