|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -import WebSocket from 'ws' |
4 | | -import http from 'http' |
5 | | -import * as number from 'lib0/number' |
6 | | -import { setupWSConnection } from './utils.js' |
| 3 | +import WebSocket from "ws"; |
| 4 | +import http from "http"; |
| 5 | +import * as number from "lib0/number"; |
| 6 | +import { setupWSConnection } from "./utils.js"; |
7 | 7 |
|
8 | | -const wss = new WebSocket.Server({ noServer: true }) |
9 | | -const host = process.env.HOST || 'localhost' |
10 | | -const port = number.parseInt(process.env.PORT || '1234') |
| 8 | +const wss = new WebSocket.Server({ noServer: true }); |
| 9 | +const host = process.env.HOST || "localhost"; |
| 10 | +const port = number.parseInt(process.env.PORT || "1234"); |
11 | 11 |
|
12 | 12 | const server = http.createServer((_request, response) => { |
13 | | - response.writeHead(200, { 'Content-Type': 'text/plain' }) |
14 | | - response.end('okay') |
15 | | -}) |
| 13 | + response.writeHead(200, { "Content-Type": "text/plain" }); |
| 14 | + response.end("okay"); |
| 15 | +}); |
16 | 16 |
|
17 | | -wss.on('connection', setupWSConnection) |
| 17 | +wss.on("connection", setupWSConnection); |
18 | 18 |
|
19 | | -server.on('upgrade', (request, socket, head) => { |
| 19 | +server.on("upgrade", (request, socket, head) => { |
20 | 20 | // You may check auth of request here.. |
21 | 21 | // Call `wss.HandleUpgrade` *after* you checked whether the client has access |
22 | 22 | // (e.g. by checking cookies, or url parameters). |
23 | 23 | // See https://github.com/websockets/ws#client-authentication |
24 | | - wss.handleUpgrade(request, socket, head, /** @param {any} ws */ ws => { |
25 | | - wss.emit('connection', ws, request) |
26 | | - }) |
27 | | -}) |
| 24 | + wss.handleUpgrade( |
| 25 | + request, |
| 26 | + socket, |
| 27 | + head, |
| 28 | + /** @param {any} ws */ (ws) => { |
| 29 | + wss.emit("connection", ws, request); |
| 30 | + } |
| 31 | + ); |
| 32 | +}); |
28 | 33 |
|
29 | 34 | server.listen(port, host, () => { |
30 | | - console.log(`running at '${host}' on port ${port}`) |
31 | | -}) |
| 35 | + console.log(`running at '${host}' on port ${port}`); |
| 36 | +}); |
0 commit comments