使用Socket.Io传输轮询错误

使用socket.io v1.3.2时出现传输错误。 这只是为了testingsocket.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 

有谁知道如何解决这个错误。 我发现这个讨论 ,但一直无法解决的问题。

       

网上收集的解决方案 "使用Socket.Io传输轮询错误"

你的代码与socket.io站点上的例子不同之处在于,socket.io示例代码正在侦听端口80,并且将端口80上的webSocket连接起来。您正在侦听端口3000,但尝试连接端口80,因此连接不起作用。 io.connect('http://localhost'); 没有指定端口号,所以它将使用端口80的默认http端口。但是,您的websocket服务器没有监听端口80,所以连接失败。

解决这个问题的最简单的方法是从这里改变客户端中的代码行:

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

对此:

 var socket = io.connect(); 

如果不填写url,则默认情况下,将使用当前网页中的域和端口(这是您所需的)。

默认情况下,本地端口是80.你连接客户端的套接字到本地主机没有声明端口。 因此默认情况下它将为您提供端口80,并且您的节点服务器正在侦听端口3000.为了让您的客户端连接到您的服务器,您必须声明服务器侦听的端口。 例如:var socket = io.connect(' http:// localhost:3000 ');