Socket.IO无法在Windows上使用Chrome和Firefox

时间:2013-06-25 13:41:04

标签: node.js google-chrome firefox websocket socket.io

我使用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

2 个答案:

答案 0 :(得分:2)

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

io.configure(function () { 
   io.set("transports", ["xhr-polling"]); 
   io.set("polling duration", 10); 
});

答案 1 :(得分:0)

我刚发现这是因为Chrome和Firefox出现问题的人的反病毒。显然它会阻止websockets甚至在这些浏览器上进行轮询。我还将端口从8080更改为8079.