javascript - Socket.Io 传输轮询错误

标签 javascript node.js socket.io

我在使用 socket.io v1.3.2 时遇到传输错误。这只是对 socket.io 的测试,所以我可以熟悉它。

我有一个文件 app.js(直接取自 socket.io 文档):

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

有谁知道如何解决这个错误。我找到了 this discussion ,但一直无法解决问题。

最佳答案

您的代码与 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 时,默认情况下,它将使用当前网页中的域和端口(这是您想要的)。

关于javascript - Socket.Io 传输轮询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28261546/

相关文章:

node.js - 使用 websocket 将 Arduino 连接到 nodejs 服务器

javascript - 如何使用 javascript 将新属性动态插入到 xml 标记中?

javascript - ember.js 中自定义输入字段的最佳实践

javascript - 如何直接链接到由 innerHTML 生成的 div?

node.js - Node js 显示不正确的时区

javascript - 按解决/执行时间的顺序解决 promise ?

javascript - Socket.io node.js Math.random 在所有用户上得到不同的结果

javascript - 给定代码中 <svg> 和 <canvas> 实现有什么区别?

node.js - PM2 重建应用程序时显示旧版本

javascript - 无法收听从羽毛服务器发送到客户端的事件