与engine.io的Nodejs服务器/客户端套接字连接

我正试图得到一个简单的套接字连接之间build立一个engine.io套接字监听器是监听端口8888和一个javascript客户端运行在一个普通的index.html

它看起来相当直接的完成这项任务,但不知何故,我无法得到一个xhr轮询客户端正确连接。 它连接,因为客户端号码增加,但onopen事件永远不会在客户端触发。 相反,服务器端的客户端数量不断增加,客户端永远不会收到来自服务器的任何消息 – 服务器也不会从客户端收到任何消息。

这一切都与websocket运输完美,但我需要xhr-polling工作以及。

app.js

var engine = require('engine.io'); var server = engine.listen(8888); server.on('connection', function (socket) { console.log(server.clientsCount); socket.send('never received by client'); // << xhr-polling client does not receive socket.on('message', function (msg) { if ('echo' == msg) socket.send(msg); }); }); 

的index.html

 <html> <head> <script src="engine.io.js"></script> <script> var socket = eio('ws://localhost:8888/'); << starts polling immediately like crazy socket.onopen = function(){ console.log('never fired'); << never sent to console socket.onmessage = function(data){}; socket.onclose = function(){}; }; </script> </head> <body> </body> </html> 

客户端控制台

 GET http://localhost:8888/engine.io/?EIO=2&transport=polling 200 OK 280ms engine.io.js (Zeile 1585) GET http://localhost:8888/engine.io/?EIO=2&transport=polling 200 OK 1ms engine.io.js (Zeile 1585) GET http://localhost:8888/engine.io/?EIO=2&transport=polling 200 OK 1ms engine.io.js (Zeile 1585) GET http://localhost:8888/engine.io/?EIO=2&transport=polling 200 OK 1ms engine.io.js (Zeile 1585) GET http://localhost:8888/engine.io/?EIO=2&transport=polling 200 OK 0ms engine.io.js (Zeile 1585) GET http://localhost:8888/engine.io/?EIO=2&transport=polling 200 OK 0ms engine.io.js (Zeile 1585) GET http://localhost:8888/engine.io/?EIO=2&transport=polling 200 OK 0ms engine.io.js (Zeile 1585) 

如果您的HTML和静态文件(JS)是来自另一个域的服务器(localhost:80,例如,作为port ==另一个“domain”)。
然后,由于安全原因,可能会拒绝WebSockets或其他stream量由于CORS原因而发生。

在您喜欢的浏览器的开发工具中使用“networking”选项卡,检查发生了什么,以及是否有任何请求失败,以及它们的标头。