react-use-websocket icon indicating copy to clipboard operation
react-use-websocket copied to clipboard

Ready state timeout not working as excpected

Open alex-frx opened this issue 8 months ago • 0 comments

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.

alex-frx avatar Jun 24 '24 12:06 alex-frx