Skip to content

AutoReconnect Websocket

Compare
Choose a tag to compare
@cmalf cmalf released this 09 Oct 06:33
· 16 commits to main since this release
b5ccfd5

Because there was a problem when the "userID" in LocalStorage.json disappeared when WebSocket Disconnected,
I moved it separately in UserId.json, and I added AutoReconnect when websocket was disconnected,
I updated it in the JavaScript or NodeJs script, for other languages ​​just adjust it yourself a little.

Using Reconnect Function

async function reconnectWebSocket() {
  const userId = await getUserIdFromFile();
  if (userId) {
    await connectWebSocket(userId);
  }
}

Implementation

  socket.onclose = () => {
    socket = null;
    console.log("WebSocket disconnected");
    stopPinging();
    reconnectWebSocket();
  };

Before Running the Script NodeJs

  • You need to install all the necessary modules
  • Run the command below
npm install ws util fs readline axios

For VPS or Running 24/7

  • This Script contains AutoLogin And AutoReconnect
  • I Set Script to AutoLogin Every 1 hour to update "access_token" and "refresh_token" updated
  • When running the script for the first time select Option 1, Log in to your account,
    Your Account Data will be stored in DataAccount.json for automatic login to work.

Update

  • Merged
  • Improvements to the Logout Section, DataAccount.json forgot not to be deleted.
          fs.unlink('DataAccount.json', (err) => {
            if (err) throw err;
          });