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