2

I am new to node.js and have got it working fine on localhost.

For localhost I am using xampp, then have a .bat file which runs node app.js (runs the server).

It works perfectly and runs the game as you can see below with both characters appearing on each screen and the positions being communicated over the server.

proof it works

Then when I try and run it on the server after using ssh to run "node app.js" it outputs info - socket.io started like it does when I run it on local host, but then when I run the app I get the following errors (I had these errors previously when trying to get it working on local host - they appear when the server doesn't run properly..):

enter image description here

Any help would be great, Thanks!

on the client side I have this code in the html file: <script src="http://localhost:8080/socket.io/socket.io.js"></script> and var socket = io.connect('http://localhost:8080');

which is then used to run all the functions. e.g.

socket.on('message', function (data) {
    var player = ig.game.getEntitiesByType( EntityPlayer )[0];
    if(player)
    {
        player.messagebox = player.messagebox + '\n' + data + ' disconnected';
    }
});  `

on the server side I have the code: var app = require('http').createServer(handler), io = require('socket.io').listen(app), fs = require('fs') app.listen(8080);

smj2393
  • 1,929
  • 1
  • 23
  • 50
  • I always run node programs using `$ node app.js`. Can you ssh onto your server, navigate to the files, and run the file? – twinlakes Jul 29 '13 at 20:07
  • done that, it seems to work fine as it outputs `info - socket.io started` I think it is something to do with the socket.io – smj2393 Jul 29 '13 at 20:09
  • are you opening a socket with the localhost when its supposed to be with your remote server? – twinlakes Jul 29 '13 at 20:15
  • I have tried the following for the io.connect section `(***.*.*.*)` , `http://localhost:8080` and `http://localhost`. It doesn't seem to work with any of them. Is this what you mean? – smj2393 Jul 29 '13 at 20:20

2 Answers2

2

Change var socket = client.connect('http://stuartjones.me:8080'); to var socket = io.connect('http://stuartjones.me:8080');.

Using http://localhost as a connection will not work. You must use your server IP address or domain.

Rhys
  • 2,055
  • 2
  • 18
  • 23
1

You must connect to your server and not to localhost. Try this var socket = io.connect('http://255.255.255.255:8080'); where you replace 255.255.255.255 with the IP address of your server. Change the address of your script too: <script src="http://255.255.255.255:8080/socket.io/socket.io.js"></script>

Are you sure that your node.js server listens to the 8080 port, that this port is opened (not blocked by a firewall), and that the socket.io module is installed (via npm) on your server?

You can see these posts for more information:

Socket.IO - require is not defined

socket.io - ReferenceError: io is not defined

Community
  • 1
  • 1
Elie Génard
  • 1,673
  • 3
  • 21
  • 34