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

时间:2014-03-04 13:53:00

标签: javascript node.js sockets websocket engine.io

我正在尝试在正在侦听端口8888的engine.io套接字侦听器和在普通index.html中运行的javascript客户端之间建立一个简单的套接字连接

看起来很直接完成这项任务,但不知怎的,我无法正确连接xhr-polling客户端。它连接,因为客户端数量增加但是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)

1 个答案:

答案 0 :(得分:0)

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

在您喜欢的浏览器的开发工具中使用网络标签,检查发生了什么,是否有任何请求失败以及标题。