diff --git a/src/server/proxy.ts b/src/server/proxy.ts index 3fd8c15..fbe522b 100644 --- a/src/server/proxy.ts +++ b/src/server/proxy.ts @@ -9,6 +9,7 @@ const upstreams = new Map, WebSocket>() interface WsData { port: number path: string + protocols: string[] } export function extractSubdomain(host: string): string | null { @@ -73,8 +74,13 @@ export function proxyWebSocket(subdomain: string, req: Request, server: Server p.trim()) : [] - const ok = server.upgrade(req, { data: { port: app.port, path } as WsData }) + const headers: Record = {} + if (protocolHeader) headers['sec-websocket-protocol'] = protocolHeader + + const ok = server.upgrade(req, { data: { port: app.port, path, protocols } as WsData, headers }) if (ok) return undefined return new Response('WebSocket upgrade failed', { status: 500 }) } @@ -82,7 +88,7 @@ export function proxyWebSocket(subdomain: string, req: Request, server: Server) { const { port, path } = ws.data - const upstream = new WebSocket(`ws://localhost:${port}${path}`) + const upstream = new WebSocket(`ws://localhost:${port}${path}`, ws.data.protocols) upstream.binaryType = 'arraybuffer' upstreams.set(ws, upstream)