node.js Socket.IO 只能在本地工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11916999/
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
Socket.IO only works locally
提问by slifty
I have this Node.JS server:
我有这个 Node.JS服务器:
var app = require('express')();
var server = app.listen(80);
var io = require('socket.io').listen(server);
var posx = 10;
var posy = 10;
app.get('/', function (req, res) {
res.sendfile( __dirname + '/index.html' );
});
io.sockets.on('connection', function (socket) {
socket.emit('start', {
x: posx,
y: posy
});
socket.on('newpos', function (data) {
posx = data["x"];
posy = data["y"];
socket.broadcast.emit('move', { x: posx, y: posy });
});
});
CLIENT SIDE CODE:
客户端代码:
var socket = io.connect('http://localhost');
socket.on('start', function (data) {
$("#pointer").animate({
'top': data["y"],
'left': data["x"]
}, 0);
});
socket.on('move', function (data) {
$("#pointer").animate({
'top': data["y"],
'left': data["x"]
}, "slow");
});
$("#pointer").draggable({
stop: function(event, ui) {
var pos = $("#pointer").position();
socket.emit('newpos', {
'x': pos.left,
'y': pos.top
});
}
});
The problem is that it seems to be only working locally. On ubuntu chrome I get:
问题是它似乎只能在本地工作。在 ubuntu chrome 上我得到:
XMLHttpRequest cannot load http://localhost/socket.io/1/?t=1344711676473. Origin http://192.168.1.130 is not allowed by Access-Control-Allow-Origin.
While on a mac, I am having a GET error for the same file...
在 mac 上,我遇到了同一个文件的 GET 错误...
Any idea on what the problem might be?
知道问题可能是什么吗?
回答by slifty
A site's domain doesn't have to do with where it is hosted, it has to do with what URL you are using to access it.
站点的域与它的托管位置无关,而与您用来访问它的 URL 相关。
Even if "192.168.1.130" and "localhost" resolve to the same server, they are considered different domains.
即使“192.168.1.130”和“localhost”解析到同一台服务器,它们也被视为不同的域。
As a result, because you have the client side code:
结果,因为您有客户端代码:
var socket = io.connect('http://localhost');
You are connecting to the domainlocalhost. If the client code was served by localhost you're fine, but if you are loading the client from another domain (for example 192.168.1.130) then you'll face problems. From the browser and server's perspective you could easily be a complete stranger trying to access that service.
您正在连接到域localhost。如果客户端代码是由 localhost 提供的,那么您可以,但是如果您从另一个域(例如 192.168.1.130)加载客户端,那么您将面临问题。从浏览器和服务器的角度来看,您很容易成为尝试访问该服务的陌生人。
To fix the problem change the client side socket creation to:
要解决此问题,请将客户端套接字创建更改为:
var socket = io.connect('192.168.1.130');
var socket = io.connect('192.168.1.130');
You should have your problem resolved.
你的问题应该得到解决。
Really though, you should just remove the parameter completely and try running:
实际上,您应该完全删除参数并尝试运行:
var socket = io.connect();
var socket = io.connect();
That way it will default to whatever domain you are based on, and it will work both on localhost, the IP, and eventually the domain name you use.
这样,它将默认为您所基于的任何域,并且它将在 localhost、IP 和最终您使用的域名上工作。
回答by Clarence
Change the app.listen(80) to app.listen(80, '192.168.1.130') for it to use that IP, that way your URLs from socket.io should be correct. Also be sure to access it from 192.168.1.130 in your browser even if you try it on the local machine.
将 app.listen(80) 更改为 app.listen(80, '192.168.1.130') 以使其使用该 IP,这样您来自 socket.io 的 URL 应该是正确的。即使您在本地计算机上尝试,也请务必在浏览器中从 192.168.1.130 访问它。

