Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

468
Visualizações
How to stop client from auto connecting when restarting server? (Socket.io)

Whenever I host a local server, connect to it with browser, then restart the server while keeping the browser open, there's a chance (Sometimes this wont happen) for the client to automatically reconnect to the server with a new socket id. Is there any way to prevent this?

I notice that even after closing the server, the error message that tells the client that there's a connection problem does not appear in the browser console log.

Also, I notice that even after restarting the server, and after the client has reconnected with a new id, it somehow still prints out the old id, that is:

//program for client
function setup(){
socket = io.connect('http://localhost:2000', {
        'reconnect': false
    });
socket.on('yourid', function(data){
            myid = data.id;
        })
//some code...
}

function draw(){
socket.on('connect_error', function (err) {
        socket.disconnect();
    });//I added this to try to manually disconnect the client whenever 
       //there's a connection problem, but it somehow doesnt work
    
console.log(myid);// this still run after shutting the server down, and gives the old id

 }

for server:

io.sockets.on('connection', function(socket) {
console.log('someone conencted, Id: ' + socket.id);
//some code...
player = new Player(socket.id);
socket.emit('yourid', {id: player.id});
}

This is driving me crazy, I tried adding server.close() when the server is about to close, but it was no use,

process.on('exit', function() {
    io.emit('disconnect');
    server.close();
});

Can anyone please help me? BTW I am using p5.js

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda