tweak proxy
This commit is contained in:
parent
96083b640f
commit
888f12a8f1
|
|
@ -9,6 +9,7 @@ const upstreams = new Map<ServerWebSocket<WsData>, 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<W
|
|||
|
||||
const url = new URL(req.url)
|
||||
const path = url.pathname + url.search
|
||||
const protocolHeader = req.headers.get('sec-websocket-protocol')
|
||||
const protocols = protocolHeader ? protocolHeader.split(',').map(p => p.trim()) : []
|
||||
|
||||
const ok = server.upgrade(req, { data: { port: app.port, path } as WsData })
|
||||
const headers: Record<string, string> = {}
|
||||
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<W
|
|||
export const websocket = {
|
||||
open(ws: ServerWebSocket<WsData>) {
|
||||
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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user