* Attaches socket.io to a server or port. * * @param srv - server or port * @param opts - options passed to engine.io * @return self
(
srv: TServerInstance | number,
opts: Partial<ServerOptions> = {}
)
| 410 | * @return self |
| 411 | */ |
| 412 | public attach( |
| 413 | srv: TServerInstance | number, |
| 414 | opts: Partial<ServerOptions> = {} |
| 415 | ): this { |
| 416 | if ("function" == typeof srv) { |
| 417 | const msg = |
| 418 | "You are trying to attach socket.io to an express " + |
| 419 | "request handler function. Please pass a http.Server instance."; |
| 420 | throw new Error(msg); |
| 421 | } |
| 422 | |
| 423 | // handle a port as a string |
| 424 | if (Number(srv) == srv) { |
| 425 | srv = Number(srv); |
| 426 | } |
| 427 | |
| 428 | if ("number" == typeof srv) { |
| 429 | debug("creating http server and binding to %d", srv); |
| 430 | const port = srv; |
| 431 | srv = http.createServer((req, res) => { |
| 432 | res.writeHead(404); |
| 433 | res.end(); |
| 434 | }); |
| 435 | srv.listen(port); |
| 436 | } |
| 437 | |
| 438 | // merge the options passed to the Socket.IO server |
| 439 | Object.assign(opts, this.opts); |
| 440 | // set engine.io path to `/socket.io` |
| 441 | opts.path = opts.path || this._path; |
| 442 | |
| 443 | this.initEngine(srv, opts); |
| 444 | |
| 445 | return this; |
| 446 | } |
| 447 | |
| 448 | public attachApp(app /*: TemplatedApp */, opts: Partial<ServerOptions> = {}) { |
| 449 | // merge the options passed to the Socket.IO server |
no test coverage detected