robtaussig / react-use-websocket

React Hook for WebSocket communication
MIT License
1.62k stars 135 forks source link

Ready state timeout not working as excpected #238

Open alex-frx opened 5 months ago

alex-frx commented 5 months ago

Im using the websocket like this

const {sendMessage, lastMessage, readyState} = useWebSocket(WEBSOCKET_URL, {
        heartbeat: {
            message: 'ping',
            returnMessage: 'pong',
            timeout: 2000,
            interval: 1000,
        },
        onClose:()=>{
            console.log("WebSocket connection closed")
        },
        shouldReconnect: (closeEvent) => {
            /*
            useWebSocket will handle unmounting for you, but this is an example of a
            case in which you would not want it to automatically reconnect
          */
            console.log("try reconnect")
           return true
        },
        reconnectAttempts: 2,
        reconnectInterval: 2000,
    });

But the heartbeat is not working like expected / described in the doc.
The heartbeat is send in the specified interval. But the timeout is not working. With these settings I would except that after 2000ms (2s) of receiving the last pong the ready state would change to closed. But this is not happing. The heartbeat continues sending producing this message in the console react-use-websocket.js?v=b1dceef9:142 WebSocket is already in CLOSING or CLOSED state. The. logging of the message start roughly after two seconds of the last received pong and continues for around a minute (the default value?) only after this the readystate is changed to closed.