我使用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上的上述浏览器中工作。
当我试图通过查看错误事件的返回数据来调试错误时,数据总是未定义的。我已经尝试了所有可能的运输方式。
这是我在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
您可以使用以下代码来解决您的问题
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
我刚刚发现这是由于Chrome和Firefox中出现问题的人的防病毒。显然,它会屏蔽网络套接字,甚至在这些浏览器上进行轮询。我还将端口从8080更改为8079。