question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebSocket closed error

See original GitHub issue

When the hearbeat interval function runs it does not check if the websocket is open and that often creates error like this one: (node:914) UnhandledPromiseRejectionWarning: Error: Can't send data because WebSocket is not opened. at exports.throwIf (/root/nodejs/ewelink-sserver/node_modules/websocket-as-promised/dist/index.js:3470:11) at WebSocketAsPromised.send (/root/nodejs/ewelink-sserver/node_modules/websocket-as-promised/dist/index.js:554:7) at Timeout._onTimeout (/root/nodejs/ewelink-sserver/node_modules/ewelink-api/mixins/websocket/openWebSocketMixin.js:38:17) at listOnTimeout (internal/timers.js:531:17) at processTimers (internal/timers.js:475:7)

Please add the check “if (wsp.isOpened)” to the interval function (line 37 in openWebSocketMixin):

   setInterval(async () => {
      if (wsp.isOpened) {
        await wsp.send('ping');
      }
    }, heartbeat);

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ttz642commented, Jan 18, 2020

See #25 for some other suggestions on how to trap errors.

If the socket isn’t open then it should return an error allowing the application to re-open the socket.

0reactions
LiYuanBrcommented, Aug 24, 2022

Hi. I solve this issue by creating an auto restart mode in the openWebSocket.js when it closes the connection.

The ewelink-api/src/mixins/openWebSocket.js in this repository: https://github.com/skydiver/ewelink-api/blob/5a07f6b6152d603c71266012177cc4b47d533bf9/src/mixins/openWebSocket.js#L38-L40

I changed to get credentials and try to restart the WebSocket:

setInterval(async () => {
      try {
        await wsp.send('ping');
      } catch (error) {
        console.error(`openWebSocket.js: ${error}`);
        console.log(`openWebSocket.js: Reconnecting...`);
        const auth = await this.getCredentials();
        const payloadLogin = wssLoginPayload({
          at: auth.at,
          apiKey: auth.user.apikey,
          appid: this.APP_ID,
        });
        await wsp.open();
        await wsp.send(payloadLogin);
      }
    }, heartbeat);

This way it closes the connection but doesn’t close the process. It tries to connect again.

I’m still learning about JavaScript and I know that this code is very primitive, however, it solved my connection problem. Feel free to make some changes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting the reason why websockets closed with close code 1006
Close Code 1006 is a special code that means the connection was closed abnormally (locally) by the browser implementation.
Read more >
[SOLVED] code server WebSocket close with status code 1006
The websocket 1006 error means that the connection has been closed, locally, by your browser. If you see a close code 1006, you...
Read more >
WebSocket.close() - Web APIs - MDN Web Docs
close () method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED , this method does nothing....
Read more >
Websocket closes unexpectedly with error 1006
I am having issues with futures websocket where I randomly get 1006 error. Some days it works perfectly fine but some days it...
Read more >
close code 1006 ("no reason") even when reason should be ...
1006 is a reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found