Server nodejs to accept connections after a running time

1

I made an application to control simultaneous logins, at start everything works perfectly, but after a few hours I can no longer connect to the server the client returns me the following error net :: ERR_CONNECTION_TIMED_OUT and on the server side nothing happens error as if it were running normally ... follow my code below:

CLIENT:

var socket;
function connect(id) {
    socket = io.connect('http://IP DO SERVER:4200');
    
    
    socket.on('connect', function (data) {
        socket.emit('join', id);
    });
    
    socket.on('messages', function (data) {
        console.log('Mensagem: ' + data.toString());

        switch (data.toString()) {
            case "kick":
                socket.close();
                console.log("Conexao fechada!");
                break;
            case "duplicate_entry":
                socket.close();
                console.log("Outro usuario ja conectado!");
                break;

        }
    });
}

SERVER:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var clients = [];

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

function logtimestamp() {
    var log_date = new Date();
    log_date = '[' + log_date.getFullYear() + '/' + (log_date.getMonth() + 1) + '/' + log_date.getDate() + ' ' + log_date.getHours() + ':' + log_date.getMinutes() + ':' + log_date.getSeconds() + ']';
    return log_date;
}// FUNCTION logtimestamp


console.log("Inicio: " + logtimestamp());
console.log("porta do server 4200")
console.log("websocket server criado!");

try {
    io.on('connection', function (client) {
        try {
            var id;
            var conexao;

            client.on('join', function (data) {
                try {
                    console.log('Client connected...'+logtimestamp()+' ID:' + data);
                    id = data;
                    conexao = {
                        ws: client,
                        id_usuario: data
                    };
                    clients.push(conexao);

                    for (var x = 0; x < clients.length; x++) {

                        //desconecta o usuario anterior
                        try {
                            //console.log(clients[x]);    
                            if (clients[x].id_usuario == id) {
                                if (clients[x].ws != conexao.ws) {
                                    //console.log(clients[x].ws);
                                    clients[x].ws.emit('messages', 'duplicate_entry');
                                    clients.splice(x, 1);
                                }
                            }
                        } catch (err) {
                            console.log("ERROR 1: " + err.message);
                        }
                    }
                } catch (err) {
                    console.log("ERROR 2: " + err.message);
                }
            });
        } catch (err) {
            console.log("ERROR 3: " + err.message);
        }
    });
} catch (err) {
    console.log("ERROR 4: " + err.message);
}
server.listen(4200); 

Has anyone ever had this problem?

    
asked by anonymous 16.11.2015 / 17:31

0 answers