Socket.io和现代浏览器不工作

我有一些与socket.io和现代浏览器奇怪的问题。 令人惊讶的是,与IE9工作正常,因为回退闪光灯似乎更好地工作。

在我的服务器(与快递)

var io = socketio.listen(server.listen(8080)); io.configure('production', function(){ console.log("Server in production mode"); io.enable('browser client minification'); // send minified client io.enable('browser client etag'); // apply etag caching logic based on version number io.enable('browser client gzip'); // gzip the file io.set('log level', 1); // reduce logging io.set('transports', [ // enable all transports (optional if you want flashsocket) 'websocket' , 'flashsocket' , 'htmlfile' , 'xhr-polling' , 'jsonp-polling' ]); }); 

在浏览器中,我可以在“networking”选项卡(在Chrome上)中看到websocket已build立,并处于挂起模式下的101 Switching Protocols 。 之后,出现xhr-polling和jsonp-polling(flashsocket发生了什么?)

最糟糕的是信息不会来回传递。 我有这个连接:

 io.sockets.on('connection', function (socket) { // If someone new comes, it will notified of the current status of the application console.log('Someone connected'); app.sendCurrentStatus(socket.id); io.sockets.emit('currentStatus', {'connected': true); }); 

并在客户端:

 socket.on('currentStatus', function (data){ console.log(data) }); 

但是,只有当我closures启动的服务器时才能看到该日志:

 NODE_ENV=production node server.js 

我究竟做错了什么?

最后,在我的头撞墙之后,我决定在几个环境中进行testing,看看它是否是防火墙的问题,因为机器在几个环境之后。

事实certificate,没有人,但我有问题,所以我检查了防病毒(趋势科技),禁用后,Chrome / Firefox能够发挥他们的魔力。

故事的道德启示

除了这里所说的–Socket.IO和防火墙软件 – 当你面对互联网上没有人似乎有问题的时候(也就是说,没有logingithub或者socket.io组),这可能是由你的防病毒软件引起的。 他们是邪恶的。 有时。

你应该让socketio在应用程序本身上听。
另外,我从来不需要使用所做的套接字来完成所有的服务器端configuration – 在大多数浏览器中,socket.io应该可以在没有这样做的情况下工作。 我会先尝试没有configuration。 此外,在服务器上,您应该从传递给callback函数的套接字发出,而不是执行io.sockets.on。

 var io = socketio.listen(app); io.sockets.on('connection', function (socket) { // If someone new comes, it will notified of the current status of the application console.log('Someone connected'); app.sendCurrentStatus(socket.id); socket.emit('currentStatus', {'connected': true); }); 

在客户端上,您需要先连接:

 var socket = io.connect(); socket.on('currentStatus', function (data){ console.log(data) }); 

如果你想看看使用socket.io进行双向通信的例子,请查看我的Nodio应用程序。

服务器端: https : //github.com/oveddan/Nodio/blob/master/lib/Utils.js

而客户端: https : //github.com/oveddan/Nodio/blob/master/public/javascripts/Instruments.js