Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

472
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!