簡體   English   中英

使用Socket.Io傳輸輪詢錯誤

[英]Transport polling error with Socket.Io

使用socket.io v1.3.2時出現傳輸錯誤。 它只是對socket.io的測試,所以我可以熟悉它。

我有一個文件app.js (直接來自socket.io docs):

var app = require('http').createServer(handler);
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(3000);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
});

和一個文件index.html

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title></title>
    <link rel="stylesheet" href="">
</head>
<body>
    <h1>Socket Test</h1>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('http://localhost');
        socket.on('news', function (data) {
            console.log(data);
        });
    </script>

</body>
</html>

在控制台中,我收到以下錯誤:

GET http://localhost/socket.io/?EIO=3&transport=polling&t=1422782880286-40 
(index):1 XMLHttpRequest cannot load http://localhost/socket.io/?EIO=3&transport=polling&t=1422782880286-40. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 404.
socket.io.js:3715 engine.io-client:socket socket error {"type":"TransportError","description":0} +37ms
socket.io.js:1402 socket.io-client:manager connect_error +38ms
socket.io.js:1402 socket.io-client:manager reconnect attempt error +1ms
socket.io.js:1402 socket.io-client:manager will wait 5000ms before reconnect attempt +0ms
socket.io.js:3715 engine.io-client:socket socket close with reason: "transport error" +2ms
socket.io.js:3715 engine.io-client:polling transport not open - deferring close +1ms
socket.io.js:1402 socket.io-client:manager attempting reconnect +5s
socket.io.js:1402 socket.io-client:manager readyState closed +0ms
socket.io.js:1402 socket.io-client:manager opening http://localhost +0ms
socket.io.js:3715 engine.io-client:socket creating transport "polling" +5s
socket.io.js:3715 engine.io-client:polling polling +0ms
socket.io.js:3715 engine.io-client:polling-xhr xhr poll +0ms
socket.io.js:3715 engine.io-client:polling-xhr xhr open GET: http://localhost/socket.io/?EIO=3&transport=polling&t=1422782885332-41 +1ms
socket.io.js:3715 engine.io-client:polling-xhr xhr data null +0ms
socket.io.js:3715 engine.io-client:socket setting transport polling +1ms
socket.io.js:1402 socket.io-client:manager connect attempt will timeout after 20000 +3ms

有誰知道如何解決此錯誤。 我發現了這個討論 ,但一直無法解決問題。

您的代碼與socket.io網站上的示例之間的區別在於socket.io示例代碼正在偵聽端口80並在端口80上連接webSocket。您正在偵聽端口3000,但嘗試在端口80上進行連接,因此連接不起作用。 io.connect('http://localhost'); 沒有指定端口號,因此它將使用默認的http端口,即端口80.但是,您的websocket服務器沒有偵聽端口80,因此連接失敗。

解決此問題的最簡單方法是更改​​客戶端中的代碼行:

var socket = io.connect('http://localhost');

對此:

var socket = io.connect();

當您省略URL時,默認情況下,它將使用當前網頁中的域和端口(這是您想要的)。

默認情況下,localhost端口為80.您將客戶端的套接字連接到localhost而不聲明端口。 因此默認情況下它為您提供了端口80,並且您的節點服務器正在偵聽端口3000.為了讓您的客戶端連接到您的服務器,您必須聲明服務器偵聽的端口。 示例:var socket = io.connect(' http:// localhost:3000 ');

暫無
暫無

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

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