【问题标题】:socket.io with node.js not working as expected带有 node.js 的 socket.io 没有按预期工作
【发布时间】:2013-10-14 17:21:22
【问题描述】:

我使用 node.js + socket.io 创建了简单的聊天应用程序。当我尝试运行它时,它没有按预期运行:它不断地从客户端发送请求。我已经在(客户端/服务器)端配置了传输设置:['jsonp-polling', 'xhr-polling']。我尝试设置['close timeout': 10],但这并没有改变任何东西。我不明白这里发生了什么。

我认为我的连接中断了,正如我在日志文件中看到的(在最后)。

以下是我的代码:

服务器端:app.js ~~~~~~~~~~~~~~~~~~~~

var sio = require('socket.io');
var io = sio.listen(app)

io.configure(function () {
    io.enable('browser client minification'); 
    io.enable('browser client etag');         
    io.enable('browser client gzip');         
    io.set('log level', 3);
});

io.on('connection', function (client) {
    client.on('message', function (msg) {
        socket.broadcast(msg);
    });
    client.on('disconnect', function () {
    });
});

客户端:index.html ~~~~~~~~~~~~~~~~~~~~~~~~

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
        <script src="http://cdn.socket.io/stable/socket.io.js"></script>
        <script>
            $(document).ready(function () {
                var socket = new io.Socket("localhost", {port: 8080});

                socket.on('connect', function () {
                    socket.send('A client connected.');
                });
                socket.on('message', function (message) {
                    $('div#messages').append($('<p>'), message);
                });
                socket.on('disconnect', function () {
                    console.log('disconnected');
                });
                socket.connect();

                $('input').keydown(function (event) {
                    if(event.keyCode === 13) {
                        socket.send($('input').val());
                        $('input').val('');
                    }
                });
            });
        </script>
    </head>
    <body>
        <input type="text" style="width: 300px;" />
        <div id="messages" style="border:solid 1px #000;">&nbsp;</div>
    </body>
</html>

这里是日志文件:

debug: client authorized
info: handshake authorized U1Y79P8WLaMz_UWoiKTc
debug: setting request GET /socket.io/1/xhr-polling/U1Y79P8WLaMz_UWoiKTc?t=1345273505128
debug: setting poll timeout
debug: client authorized for 
debug: clearing poll timeout
debug: xhr-polling writing 1::
debug: set close timeout for client U1Y79P8WLaMz_UWoiKTc
debug: setting request GET /socket.io/1/xhr-polling/U1Y79P8WLaMz_UWoiKTc?t=1345273505194
debug: setting poll timeout
debug: discarding transport
debug: cleared close timeout for client U1Y79P8WLaMz_UWoiKTc
debug: setting request GET /socket.io/1/xhr-polling/ufw4UqC2pCvWqqB5iKOQ?t=1345273505438
debug: setting poll timeout
debug: clearing poll timeout
debug: xhr-polling writing 7:::1+0
debug: set close timeout for client ufw4UqC2pCvWqqB5iKOQ
warn: client not handshaken client should reconnect
info: transport end (error)
debug: cleared close timeout for client ufw4UqC2pCvWqqB5iKOQ
debug: discarding transport
debug: client authorized
info: handshake authorized Uslh5g1YKNE8rCJDiKTd
debug: client authorized
info: handshake authorized -XHkL7Zk9KzA7WEFiKTe
debug: client authorized
info: handshake authorized _OOsM2nZOVgS93R-iKTf
debug: client authorized
info: handshake authorized IZ0vUej2iX_-TRSJiKTg
debug: setting request GET /socket.io/1/xhr-polling/IZ0vUej2iX_-TRSJiKTg?t=1345273505717
debug: setting poll timeout
debug: client authorized for 
debug: clearing poll timeout
debug: xhr-polling writing 1::
debug: set close timeout for client IZ0vUej2iX_-TRSJiKTg

谁能建议如何调试这个问题或问题究竟是什么?

【问题讨论】:

    标签: node.js socket.io


    【解决方案1】:
    io.on('connection', function (client) {
    

    应该是io.sockets.on(...

    client.on('message', function (msg) {
        socket.broadcast(msg);
    

    应该是client.broadcast(msg);socket 没有在任何地方定义)

    【讨论】:

      【解决方案2】:

      您可以根据需要配置 socket.io 设置。 https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

      我也遇到了同样的问题,发现添加下面一行就解决了问题

      io.set('transports',['xhr-polling']);
      

      【讨论】:

        猜你喜欢
        • 2020-05-24
        • 2016-03-25
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 2021-10-19
        • 2020-03-18
        • 2012-06-14
        • 2014-11-15
        相关资源
        最近更新 更多