Node.js – socket.io net::ERR_CONNECTION_TIMED_OUT

expressnode.jssocket.io

I'm testing with socket.io and unity.

In my localhost (my pc) did walk perfectly but now I got all on my hosting to see how it works online and i have the following problem:

url app online: http://almightysystem.com.ar/UnityApps/UnityChatSocket.io/

I have this loop error:

GET http://almightysystem.com.ar:29011/socket.io/?EIO=3&transport=polling&t=1423454072758-3 net::ERR_CONNECTION_TIMED_OUT

server.js:

var express = require('express');
var port = process.env.PORT || 29011;

var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.use(express.static(__dirname + '/public'));

io.on('connection', function(socket){
    socket.on('chat message', function(message){
        io.emit('chat message', message);
    });
});

http.listen(port, function(){
    console.log('Express server listening on port ' + port);
});

client.js:

//SOCKET.IO
var socket = io.connect('http://almightysystem.com.ar:29011/');

socket.on('chat message', function(message){
    SendMessage ('ChatSystem', 'GetChatMessage', message);
    console.log("Mensaje recibido del Servidor hacia Unity: " + message);
});

function UnitySendMessage(message){
    socket.emit('chat message', message);
    console.log("Mensaje de Unity al Servidor: " + message);
}

Any idea?

Best Answer

In your server code, you should use server.listen(port, ...) and not app.listen(port, ...) because app.listen() creates a new http server and attaches to that, which you do not want because you already have one (server).

Also, in your client code, you should use io.connect('http://almightysystem.com.ar:29011/'); instead of io.connect('http://almightysystem.com.ar/UnityApps:29011'); because the socket.io server won't know to listen on /UnityApps (because you're not passing a config object (with path set) to the socket.io server constructor), even after you've moved the :29011 part to the right place.

Related Topic