Aloha,我是娄琦彬,欢迎来到的我的个人网站 :-)



自称是码农界里写诗写的最好的,文学界里拍照拍的最好的, 摄影圈里喝酒喝得最优雅的,狄俄尼索斯门徒里走过的路最长的。


—— 米兰·昆德拉


这其实就是个跨源调用问题,网上的demo都只是在本地可以运行,一旦放到node云上websocket就跪了,我花了两天时间都找不到解决方案,今天去stackoverflow上搜了一下一下子就知道问题所在了,只要把io.connect(url)中的url设为空即可!!!极力推荐 stackoverflow这个网站,程序猿必备良站! 原文请见


I have this Node.JS server:

var app = require('express')();
var server = app.listen(80);
var io = require('').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) {
                    'top': data["y"],
                    'left': data["x"]
                }, 0);

            socket.on('move', function (data) {
                    'top': data["y"],
                    'left': data["x"]
                }, "slow");

                stop: function(event, ui) {
                    var pos = $("#pointer").position();

                    socket.emit('newpos', {
                        'x': pos.left,

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

XMLHttpRequest cannot load http://localhost/ Origin

2 Answers

A site's domain doesn't have to do with where it is hosted, it has to do with what domain you are using to access it.

Even if "" 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 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('');

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.

answered Aug 11 '12 at 22:41
slifty 1,6641629
oh boy... sooo stupid!!! totally forgot about that... my question is worth a delete shah – user1130217 Aug 11 '12 at 22:44
No it's an easy mistake to make! The question will help others facing similar problems. – slifty Aug 11 '12 at 22:47

Change the app.listen(80) to app.listen(80, '') for it to use that IP, that way your URLs from should be correct. Also be sure to access it from in your browser even if you try it on the local machine.

answered Aug 11 '12 at 19:51
Clarence 1,01816
no luck.. still the same error. also I don't think it'd be a good idea to put the local IP as I would like my server to be accessible from outside my intranet. – user1130217 Aug 11 '12 at 19:59
thnx man your suggestion solved my problem :) – Mj1992 Feb 16 at 11:03