V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Kasumi20
V2EX  ›  Cloudflare

请教, workers 如何中转 WebSocket

  •  
  •   Kasumi20 · 2021-10-28 09:51:12 +08:00 · 1261 次点击
    这是一个创建于 924 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的想法:

    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 抛异常了,怎么办

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   989 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 21:44 · PVG 05:44 · LAX 14:44 · JFK 17:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.