Bashevis
Bashevis

Reputation: 1565

AppFog node.js client cannot load socket.io

I just deployed a node.js site to AppFog. It works fine locally when I go to "http://localhost:8080", but when I got to the one on AppFog: http://bandwithfriends.aws.af.cm/ I get the following Chrome console error:

XMLHttpRequest cannot load http://localhost:8080/socket.io/1/?t=1357769454152. Origin http://bandwithfriends.aws.af.cm is not allowed by Access-Control-Allow-Origin.

SERVER CODE:

var app = require('http').createServer(handler),
    io = require('socket.io').listen(app),
    static = require('node-static'); // for serving files

// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');


app.listen(process.env.VCAP_APP_PORT || 8080);

io.configure('development', function(){
  io.set('transports', ['xhr-polling']);
});

CLIENT CODE index.html: I am including socket.io like this:

<script src="/socket.io/socket.io.js"></script>

CLIENT CODE script.js:

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

How do I fix this?

Upvotes: 1

Views: 2785

Answers (2)

Bashevis
Bashevis

Reputation: 1565

All I had to do to fix this was remove the url on the client side code:

var socket = io.connect();

Hope this helps anyone else having the same problem!

Upvotes: 5

shash7
shash7

Reputation: 1738

Both your client side and server side are wrong.

On the client side; make this change:

var url = io.connect('http://<your domain name>');
//For example: var url = io.connect('http://shash7.ap01.aws.af.cm');

On the server side, you need to implement socket.io like this:

var app = require('express')()   , server =
require('http').createServer(app)   , io =
require('socket.io').listen(server);

server.listen(80);

The above code was taken from the socket.io site. They changed the way socket.io listens in the 0.9 version so use the above version.

Other than that, everything looks fine. Tell me if you still can't figure this out.

Upvotes: 2

Related Questions