2

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

完成这项任务看起来相当简单,但不知何故我无法正确连接 xhr-polling 客户端。它连接,因为客户端数量增加,但客户端永远不会触发 onopen 事件。相反,服务器端的客户端数量只会不断增加,客户端永远不会从服务器接收任何消息 - 服务器也不会从客户端接收任何消息。

这一切都与 websocket 传输完美配合,但我也需要 xhr-polling 才能工作。

应用程序.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);
    });
});

索引.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)
4

1 回答 1

0

If your HTML and static files (JS) are server from another domain (localhost:80 for example, as port == another "domain").
Then due to security reasons it might deny WebSockets or other traffic to happen due to CORS reasons.

Use Network tab in Dev Tools of your favourite browser, to check what is going on and if any requests fails as well as their headers.

于 2014-03-05T17:21:03.407 回答