我的想法:
async function handleRequest(request) {
const webSocketPair = new WebSocketPair()
const [client, server] = Object.values(webSocketPair)
server.accept()
const proxy = new WebSocket('wss://abc.com:9443'); // 这个 API 不能用
proxy.onmessage = msg => {
client.send(msg);
}
server.onmessage = msg => {
proxy.send(msg);
}
return new Response(null, {
status: 101,
webSocket: client
});
}
new WebSocket 抛异常了,怎么办