Firing window.close from within socket.on('disconnect'

68 views Asked by At

Everyone I am learning javascript so apologies in advance if my questions is appears ignorant.

I am using sockets to write an on-line interactive quiz app. It is working well. However, I have found that if a users mobile device goes into auto-lock mode then the websocket disconnects, even though when a user touches the device again the screen is still available. I don't want to reconnect a socket, as I am using socket.id to establish an array of online users.

Sorry for the preamble - here is my question. Is there a way to programatically close the window once a websocket is disconnected? Some example code that I have attempted below. This throws an exception: "ReferenceError: window is not defined" I know the window. commands works outside of a socket.on function, so assume it cannot be called within.

Anyway, I would welcome any insight, advice and examples on a resolution to help me with my learning.

Many thanks, SJM72

Code extract:

socket.on('disconnect', () => {
    const player_name = getUserName(socket.id);

    console.log(player_name, " Has decided to leave the session");
    
    window.open('', '_self', ''); window.close();
});
0

There are 0 answers