* 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> = {},
)
| 480 | * @return self |
| 481 | */ |
| 482 | public attach( |
| 483 | srv: TServerInstance | number, |
| 484 | opts: Partial<ServerOptions> = {}, |
| 485 | ): this { |
| 486 | if (class="st">"function" == typeof srv) { |
| 487 | const msg = |
| 488 | class="st">"You are trying to attach socket.io to an express " + |
| 489 | class="st">"request handler function. Please pass a http.Server instance."; |
| 490 | throw new Error(msg); |
| 491 | } |
| 492 | |
| 493 | class="cm">// handle a port as a string |
| 494 | if (Number(srv) == srv) { |
| 495 | srv = Number(srv); |
| 496 | } |
| 497 | |
| 498 | if (class="st">"number" == typeof srv) { |
| 499 | debug(class="st">"creating http server and binding to %d", srv); |
| 500 | const port = srv; |
| 501 | srv = createServer((_req, res) => { |
| 502 | res.writeHead(404); |
| 503 | res.end(); |
| 504 | }); |
| 505 | srv.listen(port); |
| 506 | } |
| 507 | |
| 508 | class="cm">// merge the options passed to the Socket.IO server |
| 509 | Object.assign(opts, this.opts); |
| 510 | class="cm">// set engine.io path to `/socket.io` |
| 511 | opts.path = opts.path || this._path; |
| 512 | |
| 513 | this.initEngine(srv, opts); |
| 514 | |
| 515 | return this; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Attaches socket.io to a uWebSockets.js app. |
no test coverage detected