* Initialize websocket server * * @protected
()
| 713 | * @protected |
| 714 | */ |
| 715 | protected init() { |
| 716 | if (!~this.opts.transports.indexOf("websocket")) return; |
| 717 | |
| 718 | if (this.ws) this.ws.close(); |
| 719 | |
| 720 | this.ws = new this.opts.wsEngine({ |
| 721 | noServer: true, |
| 722 | clientTracking: false, |
| 723 | perMessageDeflate: this.opts.perMessageDeflate, |
| 724 | maxPayload: this.opts.maxHttpBufferSize, |
| 725 | }); |
| 726 | |
| 727 | if (typeof this.ws.on === "function") { |
| 728 | this.ws.on("headers", (headersArray, req: EngineRequest) => { |
| 729 | // note: 'ws' uses an array of headers, while Engine.IO uses an object (response.writeHead() accepts both formats) |
| 730 | // we could also try to parse the array and then sync the values, but that will be error-prone |
| 731 | const additionalHeaders = req[kResponseHeaders] || {}; |
| 732 | delete req[kResponseHeaders]; |
| 733 | |
| 734 | const isInitialRequest = !req._query.sid; |
| 735 | if (isInitialRequest) { |
| 736 | this.emit("initial_headers", additionalHeaders, req); |
| 737 | } |
| 738 | |
| 739 | this.emit("headers", additionalHeaders, req); |
| 740 | |
| 741 | debug("writing headers: %j", additionalHeaders); |
| 742 | Object.keys(additionalHeaders).forEach((key) => { |
| 743 | headersArray.push(`${key}: ${additionalHeaders[key]}`); |
| 744 | }); |
| 745 | }); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | protected cleanup() { |
| 750 | if (this.ws) { |
no test coverage detected