2

我使用 node.js (0.10.10)、socket.io (0.9.16) 和 Express (3.2.6) 制作了一个类似 Facebook 的聊天服务器和客户端。它在包括所有版本的 Internet Explorer 在内的所有浏览器中都能正常工作,但 Windows 上的 Firefox (21.0) 和 Chrome (27.0.1453.116) 就无法正常工作。它确实适用于 OS X 上的上述浏览器。

当我尝试通过查看错误事件的返回数据来调试错误时,数据始终未定义。我已经尝试了所有可能的运输方式。

这是我用于在 Windows 中的 Chrome 和 Firefox 上测试的聊天应用程序的简化代码:

客户:

<script src="http://www.xserverx.com:8080/socket.io/socket.io.js"></script>
<script>
    var ioUrl = 'http://www.xserverx.eu:8080';
    var socket = io.connect(ioUrl, {
        'reconnect': true,
        'reconnection delay': 1500,
        //'sync disconnect on unload': true
    });

    $('#send-button').click(function() {
        socket.emit('message', { message: $('#message').val() });
    });

    // Event listeners
    socket.on('connect', function() {
        // Display socket transport type
        // Is empty in Chrome and Firefox on Windows
        $('#transport-type').html(socket.socket.transport.name);
    });

    socket.on('onlinechange', function(data) {
        $('#users-online').html(data.online);
    });

    socket.on('message', function(data) {
        $('#chat').append('<p>' + data.message + '</p>');
    });

    socket.on('error', function(data) {
        $('#errors').html('Error: Error. ' + JSON.stringify(data));
    });
    socket.on('connect_failed', function(data) {
        $('#errors').html('Error: Connect failed. ' + JSON.stringify(data));
    });
    socket.on('reconnect_failed', function(data) {
        $('#errors').html('Error: Reconnect failed. ' + JSON.stringify(data));
    });
</script>

服务器:

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

var port = 8080;

var io = sio.listen(app.listen(port));
    io.set('log level', 1); // Only show warnings and errors
    io.set('polling duration', 10);
    io.enable('browser client minification');
    io.enable('browser client gzip');


var totalOnline = 0;

// Server
io.sockets.on('connection', function(socket) {
    socket.setMaxListeners(0);

    console.log('---- New user online ----');
    totalOnline++;
    socket.emit('onlinechange', { online: totalOnline });

    socket.on('disconnect', function() {
        console.log('---- User disconnected ----');

        totalOnline--;

        socket.emit('onlinechange', { online: totalOnline });
    });

    socket.on('message', function(data) {
        io.sockets.emit('message', { message: data.message });
    });
});
console.log('Listening on port ' + port);

这是在 Windows 中将单个用户与 Chrome 连接时来自服务器的调试日志:

Listening on port 8080
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized NnDQmjFbOsc37kSRdPCg
   debug - setting request GET /socket.io/1/websocket/NnDQmjFbOsc37kSRdPCg
   debug - set heartbeat interval for client NnDQmjFbOsc37kSRdPCg
   debug - client authorized for 
   debug - websocket writing 1::
---- New user online ----
   debug - websocket writing 5:::{"name":"onlinechange","args":[{"online":1}]}
   debug - setting request GET /socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166541799
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared heartbeat interval for client NnDQmjFbOsc37kSRdPCg
   debug - clearing poll timeout
   debug - xhr-polling writing 8::
   debug - set close timeout for client NnDQmjFbOsc37kSRdPCg
   debug - xhr-polling closed due to exceeded duration
   debug - setting request GET /socket.io/1/jsonp-polling/NnDQmjFbOsc37kSRdPCg?t=1372166551799&i=0
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client NnDQmjFbOsc37kSRdPCg
   debug - setting request GET /socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166551918
   debug - setting poll timeout
   debug - discarding transport
   debug - clearing poll timeout
   debug - clearing poll timeout
   debug - xhr-polling writing 8::
   debug - set close timeout for client NnDQmjFbOsc37kSRdPCg
   debug - xhr-polling closed due to exceeded duration
   debug - setting request GET /socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166562023
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared close timeout for client NnDQmjFbOsc37kSRdPCg

这是来自初始/socket.io/1/?t=1372166652973文件的响应:

NnDQmjFbOsc37kSRdPCg:60:60:websocket,htmlfile,xhr-polling,jsonp-polling

这是 Chrome 请求日志的一部分:

XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/?t=1372166531543". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166541799". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166551918". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166562023". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166572144". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166582249". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166592351". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166602445". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166612595". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166622697". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166632801". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166642900". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166653115". socket.io.js:2
GET http://www.xserverx.com:8080/socket.io/1/jsonp-polling/NnDQmjFbOsc37kSRdPCg?t=1372166551799&i=0  socket.io.js:2 (FAILED)
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166663203". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166673295". socket.io.js:2
XHR finished loading: "http://www.xserverx.com:8080/socket.io/1/xhr-polling/NnDQmjFbOsc37kSRdPCg?t=1372166683405". socket.io.js:2
4

2 回答 2

2

您可以使用以下代码来解决您的问题

io.configure(function () { 
   io.set("transports", ["xhr-polling"]); 
   io.set("polling duration", 10); 
});
于 2013-08-29T14:56:25.277 回答
0

我刚刚发现这是由于在 Chrome 和 Firefox 中遇到问题的人的防病毒。显然它会阻止 websocket 甚至在这些浏览器上进行轮询。我还将端口从 8080 更改为 8079。

于 2013-06-26T12:05:25.760 回答