smj2393
smj2393

Reputation: 1957

How do I get node.js working on my server?

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

Upvotes: 2

Views: 1264

Answers (2)

Elie G&#233;nard
Elie G&#233;nard

Reputation: 1683

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

Upvotes: 1

Rhys
Rhys

Reputation: 2173

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.

Upvotes: 2

Related Questions