【问题标题】:socket.io - multiple connections causing slow message emits to serversocket.io - 多个连接导致缓慢的消息发送到服务器
【发布时间】:2018-08-04 18:18:00
【问题描述】:

注意:当我删除“node_modules\uws\uws_win32_59.node”时,它工作正常。 uws被engine.io使用,socket.io使用。

我编写了一个非常基本的应用程序来演示这个问题。在下面的应用程序中,在 chrome 中打开了 index.html 的 2 个选项卡,单击“emit”会从客户端发出消息,但需要很长一段时间才能到达服务器。 2-15+秒的任何地方。如果我只打开 1 个 index.html 页面,它可以正常工作,但是一旦打开第二个页面,我就会遇到问题。如果我删除上面的 uws_win32_59.node,它可以很好地处理多个连接。

server.js

var io = require('socket.io')();

io.on('connection', function(socket){
    console.log('connection made');
    socket.on('number', function(num) {
        console.log(num + ' received on server');
        io.emit('number', num);
        console.log(num + ' emitted from server');
    });
});

io.listen(9001);

index.html

<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
    </head>
    <body>
        <button id="emit">emit</button>

        <p id="nums"></p>

        <script>
                var socket = io('http://localhost:9001');    

                socket.on('number', function(num){
                        console.log(num + ' received on client');
                        document.getElementById('nums').innerHTML = num;
                    });      

                var num = 0;
                document.getElementById('emit').addEventListener('click', function(){                    
                    num++;
                    socket.emit('number', num);
                    console.log(num + ' emitted from client');
                });
        </script>
    </body>
</html>

默认的“npm install socket.io”会安装上面提到的那个uws模块,它本身就是engine.io使用的。

运行“节点服务器”并打开 index.html 的 2 个实例并单击发出并注意浏览器开发人员工具控制台日志记录和节点服务器控制台日志记录以重新创建问题。

编辑:Windows 操作系统上的 uws 似乎存在一个未解决的问题:https://github.com/socketio/socket.io/issues/3100

【问题讨论】:

    标签: node.js socket.io engine.io


    【解决方案1】:

    也许你需要 npm install socket.io@1.7.4。我认为 2.0 版的速度要慢得多

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 2015-07-09
      • 2020-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多