* Attaches socket.io to a uWebSockets.js app. * @param app * @param opts
(app /*: TemplatedApp */, opts: Partial<ServerOptions> = {})
| 521 | * @param opts |
| 522 | */ |
| 523 | public attachApp(app /*: TemplatedApp */, opts: Partial<ServerOptions> = {}) { |
| 524 | class="cm">// merge the options passed to the Socket.IO server |
| 525 | Object.assign(opts, this.opts); |
| 526 | class="cm">// set engine.io path to `/socket.io` |
| 527 | opts.path = opts.path || this._path; |
| 528 | |
| 529 | class="cm">// initialize engine |
| 530 | debug(class="st">"creating uWebSockets.js-based engine with opts %j", opts); |
| 531 | const engine = new uServer(opts); |
| 532 | |
| 533 | engine.attach(app, opts); |
| 534 | |
| 535 | class="cm">// bind to engine events |
| 536 | this.bind(engine); |
| 537 | |
| 538 | if (this._serveClient) { |
| 539 | class="cm">// attach static file serving |
| 540 | app.get(`${this._path}/*`, (res, req) => { |
| 541 | if (!this.clientPathRegex.test(req.getUrl())) { |
| 542 | req.setYield(true); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | const filename = req |
| 547 | .getUrl() |
| 548 | .replace(this._path, class="st">"") |
| 549 | .replace(/\?.*$/, class="st">"") |
| 550 | .replace(/^\class="cm">//, class="st">""); |
| 551 | const isMap = dotMapRegex.test(filename); |
| 552 | const type = isMap ? class="st">"map" : class="st">"source"; |
| 553 | |
| 554 | class="cm">// Per the standard, ETags must be quoted: |
| 555 | class="cm">// https://tools.ietf.org/html/rfc7232#section-2.3 |
| 556 | const expectedEtag = class="st">'"' + clientVersion + class="st">'"'; |
| 557 | const weakEtag = class="st">"W/" + expectedEtag; |
| 558 | |
| 559 | const etag = req.getHeader(class="st">"if-none-match"); |
| 560 | if (etag) { |
| 561 | if (expectedEtag === etag || weakEtag === etag) { |
| 562 | debug(class="st">"serve client %s 304", type); |
| 563 | res.writeStatus(class="st">"304 Not Modified"); |
| 564 | res.end(); |
| 565 | return; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | debug(class="st">"serve client %s", type); |
| 570 | |
| 571 | res.writeHeader(class="st">"cache-control", class="st">"public, max-age=0"); |
| 572 | res.writeHeader( |
| 573 | class="st">"content-type", |
| 574 | class="st">"application/" + (isMap ? class="st">"json" : class="st">"javascript") + class="st">"; charset=utf-8", |
| 575 | ); |
| 576 | res.writeHeader(class="st">"etag", expectedEtag); |
| 577 | |
| 578 | const filepath = path.join(__dirname, class="st">"../client-dist/", filename); |
| 579 | serveFile(res, filepath); |
| 580 | }); |
no test coverage detected