|
| 1 | +import { IncomingMessage, Server, createServer } from 'node:http'; |
| 2 | +import { v4 as uuid } from 'uuid'; |
| 3 | +import { WebSocket, WebSocketServer } from 'ws'; |
| 4 | + |
| 5 | +const DEFAULT_PORT = 51_040; |
| 6 | + |
| 7 | +export class WsServerManager { |
| 8 | + |
| 9 | + server: Server; |
| 10 | + port?: number; |
| 11 | + |
| 12 | + private wsServerMap = new Map<string, WebSocketServer>(); |
| 13 | + private handlerMap = new Map<string, (ws: WebSocket, manager: WsServerManager, request: IncomingMessage) => void>(); |
| 14 | + |
| 15 | + private connectionSecret; |
| 16 | + |
| 17 | + constructor(connectionSecret?: string) { |
| 18 | + this.server = createServer(); |
| 19 | + this.connectionSecret = connectionSecret; |
| 20 | + this.wsServerMap.set('default', this.createWssServer()); |
| 21 | + |
| 22 | + this.initServer(); |
| 23 | + } |
| 24 | + |
| 25 | + listen(cb?: () => void, port?: number, ) { |
| 26 | + this.port = port ?? DEFAULT_PORT |
| 27 | + this.server.listen(this.port, 'localhost', cb); |
| 28 | + } |
| 29 | + |
| 30 | + setDefaultHandler(handler: (ws: WebSocket, manager: WsServerManager) => void): WsServerManager { |
| 31 | + const wss = this.createWssServer(); |
| 32 | + this.wsServerMap.set('default', wss); |
| 33 | + this.handlerMap.set('default', handler); |
| 34 | + |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + addAdditionalHandlers(path: string, handler: (ws: WebSocket) => void): WsServerManager { |
| 39 | + this.handlerMap.set(path, () => { |
| 40 | + const wss = this.addWebsocketServer(); |
| 41 | + |
| 42 | + }); |
| 43 | + |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + startAdhocWsServer(sessionId: string, handler: (ws: WebSocket, manager: WsServerManager) => void) { |
| 48 | + this.wsServerMap.set(sessionId, this.createWssServer()); |
| 49 | + this.handlerMap.set(sessionId, handler); |
| 50 | + } |
| 51 | + |
| 52 | + private addWebsocketServer(): string { |
| 53 | + const key = uuid(); |
| 54 | + |
| 55 | + const wss = new WebSocketServer({ |
| 56 | + noServer: true |
| 57 | + }) |
| 58 | + this.wsServerMap.set(key, wss); |
| 59 | + |
| 60 | + wss.on('close', () => { |
| 61 | + this.wsServerMap.delete(key); |
| 62 | + }) |
| 63 | + |
| 64 | + return key; |
| 65 | + } |
| 66 | + |
| 67 | + private initServer() { |
| 68 | + this.server.on('upgrade', (request, socket, head) => { |
| 69 | + console.log('upgrade') |
| 70 | + |
| 71 | + const { pathname } = new URL(request.url!, 'ws://localhost:51040') |
| 72 | + console.log('Pathname:', pathname) |
| 73 | + |
| 74 | + const code = request.headers['sec-websocket-protocol'] |
| 75 | + if (this.connectionSecret && code !== this.connectionSecret) { |
| 76 | + console.log('Auth failed'); |
| 77 | + socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n') |
| 78 | + socket.destroy() |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (pathname === '/' && this.handlerMap.has('default')) { |
| 83 | + const wss = this.wsServerMap.get('default'); |
| 84 | + wss?.handleUpgrade(request, socket, head, (ws, request) => this.handlerMap.get('default')!(ws, this, request)); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + const pathSections = pathname.split('/').filter(Boolean); |
| 89 | + console.log(pathSections); |
| 90 | + console.log('available sessions', this.handlerMap) |
| 91 | + |
| 92 | + if (pathSections[0] === 'session' |
| 93 | + && pathSections[1] |
| 94 | + && this.handlerMap.has(pathSections[1]) |
| 95 | + ) { |
| 96 | + const sessionId = pathSections[1]; |
| 97 | + console.log('session found, upgrading', sessionId); |
| 98 | + |
| 99 | + const wss = this.wsServerMap.get(sessionId)!; |
| 100 | + |
| 101 | + wss.handleUpgrade(request, socket, head, (ws, request) => this.handlerMap.get(sessionId)!(ws, this, request)); |
| 102 | + return; |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | + |
| 107 | + private createWssServer(): WebSocketServer { |
| 108 | + return new WebSocketServer({ |
| 109 | + noServer: true, |
| 110 | + }) |
| 111 | + } |
| 112 | +} |
0 commit comments