简体   繁体   English

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

[英]Nodejs server/client socket connection with engine.io

I am trying to get a simple socket connection established between an engine.io socket listener which is listening on port 8888 and a javascript client which is running in a plain index.html 我正在尝试在正在端口8888上侦听的engine.io套接字侦听器与在纯index.html中运行的javascript客户端之间建立一个简单的套接字连接

It looked rather straight forward to accomplish this task but somehow I am unable to get a xhr-polling client connected properly. 它看起来很简单,可以完成此任务,但是以某种方式我无法正确连接xhr-polling客户端。 It connects, since the client number increases but the onopen event is never triggered on the client side. 它连接,因为客户端数量增加,但是onopen事件永远不会在客户端触发。 Instead the client count on the server side just keeps increasing infinitely and the client is never receiving any messages from the server - nor is the server receiving any messages from the client. 取而代之的是,服务器端的客户端数量一直在无限增长,并且客户端从未从服务器接收任何消息-服务器也没有从客户端接收任何消息。

It all works perfectly with the websocket transport, but I need xhr-polling to work as well. 一切都与websocket传输完美配合,但是我还需要xhr-polling才能正常工作。

app.js 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 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>

client console 客户端控制台

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)

If your HTML and static files (JS) are server from another domain (localhost:80 for example, as port == another "domain"). 如果您的HTML和静态文件(JS)是来自另一个域的服务器(例如localhost:80,作为端口==另一个“域”)。
Then due to security reasons it might deny WebSockets or other traffic to happen due to CORS reasons. 然后由于安全原因,它可能会由于CORS原因而拒绝WebSocket或其他流量的发生。

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. 使用您喜欢的浏览器的“开发工具”中的“网络”选项卡,检查正在发生的情况以及是否有任何请求失败以及其标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM