skydiver / ewelink-api

eWeLink API for JavaScript
https://www.npmjs.com/package/ewelink-api
MIT License
264 stars 108 forks source link

problem with setInterval wsp.send('ping') openWebSocket.js #209

Open paliuca opened 1 year ago

paliuca commented 1 year ago

Hello, I encountering a problem with this setInterval located at the end of the openWebSocket.js file located in ewelink-api/src/mixins/ setInterval(async () => { await wsp.send('ping'); }, heartbeat); The problem is that the send function in the WebSocketAsPromised is trowing an error (when the socket is not opened ) which is not handled in the openWebSocket.js so the node js crashes. i fixed it by replacing those 3 lines with:

    const intervalID = setInterval(async () => {
        try {
            await wsp.send('ping');
        } catch (error) {
            clearInterval(intervalID);
        }
    }, heartbeat);

Is it possible to to that? or I am doing something wrong. Thank you

julynessi commented 1 year ago

Have same problem. Anything that make websocket cannot communicate to coolkit server eg. computer sleep, internet connection lost ... It's always make node app crash .. because no exception handle in openWebSocket.js ..... I can handle this problem without changing the "ewelink-api" module by use "pm2' to run app and if it's crash it's automatic restart. hope this issue fix in the future......in fact before pm2 I try to set heartbeat to maximum of int32 and manage to send ping myself It's not survive, then no ping send at all....but the web socket still send ping (I observed with onSend callback) anyway if you need some solution to handle this problem I prefer "pm2" to run the app and automatic restart if it's crash.