1

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();
});
SJM72
  • 11
  • 1
  • 2
    this is a browser, or something like Electron? Because if it's a browser, it's infinitely better to not close windows but instead set the content to something that tells the user they can close the tab/window. If it's electron or similar GUI solution, you'll want to check their documentation for non-standard-web-API calls that you can use instead for window management. – Mike 'Pomax' Kamermans May 12 '21 at 16:47

0 Answers0