* @param {import("../Server.js").default} server server
(server)
| 11 | * @param {import("../Server.js").default} server server |
| 12 | */ |
| 13 | constructor(server) { |
| 14 | super(server); |
| 15 | |
| 16 | /** @type {import("ws").ServerOptions} */ |
| 17 | const options = { |
| 18 | .../** @type {WebSocketServerConfiguration} */ |
| 19 | (this.server.options.webSocketServer).options, |
| 20 | clientTracking: false, |
| 21 | }; |
| 22 | const isNoServerMode = |
| 23 | typeof options.port === "undefined" && |
| 24 | typeof options.server === "undefined"; |
| 25 | |
| 26 | if (isNoServerMode) { |
| 27 | options.noServer = true; |
| 28 | } |
| 29 | |
| 30 | this.implementation = new WsServer(options); |
| 31 | |
| 32 | /** @type {import("http").Server} */ |
| 33 | (this.server.server).on( |
| 34 | "upgrade", |
| 35 | /** |
| 36 | * @param {import("http").IncomingMessage} req request |
| 37 | * @param {import("stream").Duplex} sock socket |
| 38 | * @param {Buffer} head head |
| 39 | */ |
| 40 | (req, sock, head) => { |
| 41 | if (!this.implementation.shouldHandle(req)) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | this.implementation.handleUpgrade(req, sock, head, (connection) => { |
| 46 | this.implementation.emit("connection", connection, req); |
| 47 | }); |
| 48 | }, |
| 49 | ); |
| 50 | |
| 51 | this.implementation.on( |
| 52 | "error", |
| 53 | /** |
| 54 | * @param {Error} err error |
| 55 | */ |
| 56 | (err) => { |
| 57 | this.server.logger.error(err.message); |
| 58 | }, |
| 59 | ); |
| 60 | |
| 61 | const interval = setInterval(() => { |
| 62 | for (const client of this.clients) { |
| 63 | if (client.isAlive === false) { |
| 64 | client.terminate(); |
| 65 | |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | client.isAlive = false; |
| 70 | client.ping(() => {}); |