Node.js-WS模块错误

问题描述

我试图做的是制作一个可以连接到websocket服务器的websocket代理。如果您仅连接一次服务器,它将起作用。如果此后多次尝试,它将崩溃并给出错误Error: server.handleUpgrade() was called more than once with the same socket,possibly due to a misconfiguration。我尝试在其上设置noServer,但这使websocket代理不起作用。这是我的代码。我真的很乐意提供帮助!

    const WebSocket = require('ws'),https = require('https'),http = require('http');

const server_options = {
        key: fs.readFileSync('./ssl/default.key'),cert: fs.readFileSync('./ssl/default.crt')
      }

    const server = https.createServer(server_options,app);

    server.on('upgrade',function upgrade(req,socket,head) {

      const wss = new WebSocket.Server({ 
        server: server
       });

      wss.on('connection',function connection(ws) {
        
        console.log(req.url.slice(1))

        // If the URL pathname is "/wss://echo.websocket.org". The proxy would connect to "wss://echo.websocket.org"

        const client = new WebSocket(req.url.slice(1));
  
        ws.on('message',function incoming(message) {
   
          client.send(message)

        });
      
        client.on('message',function incoming(data) {

         ws.send(data)

        })

      }); 

    });

server.listen(9000);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)