Javascript 使用 Socket.Io 传输轮询错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28261546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transport polling error with Socket.Io
提问by fraxture
I'm getting a transport error when using socket.io v1.3.2. It's just meant to be a test for socket.io so I can familiarize myself with it.
使用 socket.io v1.3.2 时出现传输错误。它只是为了测试socket.io,所以我可以熟悉它。
I have a file app.js(taken directly from socket.io docs):
我有一个文件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' });
});
And a file index.html:
和一个文件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>
In the console, I get the following error:
在控制台中,我收到以下错误:
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
Does anyone know how to fix this error. I found this discussion, but have been unable to solve the problem.
有谁知道如何解决这个错误。我找到了这个讨论,但一直无法解决问题。
回答by jfriend00
The difference between your code and the sample on socket.io's site is that the socket.io example code is listening on port 80 and connecting the webSocket on port 80. You are listening on port 3000, but trying to connect on port 80, thus the connection does not work. io.connect('http://localhost');does not specify a port number so it will use the default http port which is port 80. But, your websocket server is not listening on port 80 so the connection fails.
您的代码与 socket.io 站点上的示例之间的区别在于 socket.io 示例代码正在侦听端口 80 并在端口 80 上连接 webSocket。您正在侦听端口 3000,但尝试在端口 80 上连接,因此连接不起作用。 io.connect('http://localhost');未指定端口号,因此它将使用默认的 http 端口,即端口 80。但是,您的 websocket 服务器未在端口 80 上侦听,因此连接失败。
The simplest way to fix this is to change the line of code in the client from this:
解决此问题的最简单方法是更改客户端中的代码行:
var socket = io.connect('http://localhost');
to this:
对此:
var socket = io.connect();
When you leave out the URL, it will, by default, use the domain and port from the current web page (which is what you want).
当您省略 URL 时,默认情况下,它将使用当前网页中的域和端口(这是您想要的)。
回答by Louie Aguilar
by default, the localhost port is 80. you connected the client's socket to localhost without declaring the port. so it gave you the port 80 by default and your node server is listening to port 3000. in order for your client to connect to your server, you must declare the port which server listens. example: var socket = io.connect('http://localhost:3000');
默认情况下,本地主机端口是 80。您将客户端的套接字连接到本地主机,而没有声明端口。因此默认情况下它为您提供了端口 80,并且您的节点服务器正在侦听端口 3000。为了让您的客户端连接到您的服务器,您必须声明服务器侦听的端口。例如:var socket = io.connect(' http://localhost:3000');

