Socket.IO 只能在本地工作 [英] Socket.IO only works locally

查看:25
本文介绍了Socket.IO 只能在本地工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 Node.JS 服务器:

I have this Node.JS server:

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 });
    });
});

客户端代码:

            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
                    });
                }
            });

问题是它似乎只能在本地工作.在 ubuntu chrome 上我得到:

The problem is that it seems to be only working locally. On ubuntu chrome I get:

XMLHttpRequest cannot load http://localhost/socket.io/1/?t=1344711676473. Origin http://192.168.1.130 is not allowed by Access-Control-Allow-Origin.

在 mac 上,我遇到同一个文件的 GET 错误...

While on a mac, I am having a GET error for the same file...

知道可能是什么问题吗?

Any idea on what the problem might be?

推荐答案

站点的域与其托管位置无关,而与您用来访问它的 URL 相关.

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.

即使192.168.1.130"和localhost"解析到同一个服务器,它们也被认为是不同的域.

Even if "192.168.1.130" and "localhost" resolve to the same server, they are considered different domains.

结果,因为你有客户端代码:

As a result, because you have the client side code:

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

您正在连接到 本地主机.如果客户端代码是由 localhost 提供的,那你没问题,但如果你从另一个域(例如 192.168.1.130)加载客户端,那么你会遇到问题.从浏览器和服务器的角度来看,您很容易成为尝试访问该服务的陌生人.

You are connecting to the domain localhost. 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.

要解决此问题,请将客户端套接字创建更改为:

To fix the problem change the client side socket creation to:

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();

这样它就会默认使用您所基于的任何域,并且它可以在本地主机、IP 以及您使用的域名上运行.

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.

这篇关于Socket.IO 只能在本地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆