Socket.IO only works locally

2019-03-12 09:46发布

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

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:

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...

Any idea on what the problem might be?

2条回答
该账号已被封号
2楼-- · 2019-03-12 10:09

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.

查看更多
走好不送
3楼-- · 2019-03-12 10:10

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.

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

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

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.

查看更多
登录 后发表回答