WebSocket closed error
See original GitHub issueWhen 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:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
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.
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:
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.