Server nodejs stops accepting connections after a running time

Asked

Viewed 272 times

2

I made an application to control simultaneous logins, in starts everything works perfectly, but after a few hours I can’t connect to the server the client returns me the following error net::ERR_CONNECTION_TIMED_OUT and on the server side no error happens as if it was 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); 

Someone’s had this problem before?

  • I was able to solve the error by switching server, I was using hostgator, I switched to digitalocean and solved...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.