tweak proxy
This commit is contained in:
parent
96083b640f
commit
888f12a8f1
|
|
@ -9,6 +9,7 @@ const upstreams = new Map<ServerWebSocket<WsData>, WebSocket>()
|
||||||
interface WsData {
|
interface WsData {
|
||||||
port: number
|
port: number
|
||||||
path: string
|
path: string
|
||||||
|
protocols: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractSubdomain(host: string): string | null {
|
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 url = new URL(req.url)
|
||||||
const path = url.pathname + url.search
|
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
|
if (ok) return undefined
|
||||||
return new Response('WebSocket upgrade failed', { status: 500 })
|
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 = {
|
export const websocket = {
|
||||||
open(ws: ServerWebSocket<WsData>) {
|
open(ws: ServerWebSocket<WsData>) {
|
||||||
const { port, path } = ws.data
|
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'
|
upstream.binaryType = 'arraybuffer'
|
||||||
upstreams.set(ws, upstream)
|
upstreams.set(ws, upstream)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user