Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

475
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda