簡體   English   中英

Node.js服務器/客戶端套接字與engine.io的連接

[英]Nodejs server/client socket connection with 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)

如果您的HTML和靜態文件(JS)是來自另一個域的服務器(例如localhost:80,作為端口==另一個“域”)。
然后由於安全原因,它可能會由於CORS原因而拒絕WebSocket或其他流量的發生。

使用您喜歡的瀏覽器的“開發工具”中的“網絡”選項卡,檢查正在發生的情況以及是否有任何請求失敗以及其標頭。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM