Home > OS >  Send message to disconnected user?
Send message to disconnected user?

Time:10-24

so, as you may know there is an admin-ui library (Idk what to call it) in socket.io.
what I want to do is if a client is kicked, send a message to that client (or have the client detect when it is disconnected)
i have tried:

socket.on('disconnect', function(){
  alert('disconnected');
});

but that does nothing...

CodePudding user response:

Found a fix using the socket attribute connected and setting an interval to check if the client is connected to a socket:

// client
let offline_alerted = false;
setInterval(function () {
    if (socket.connected === false) {
        if (offline_alerted === false) {
            alert("Disconnected :(");
            offline_alerted = true;
        }
    } else if (offline_alerted === true) {
        alert("Reconnected!");
        offline_alerted = false;
    }
}, 1000);
  • Related