← All Exercises
Decide WebSocket Reconnect from Heartbeat Misses
basicjavascript
Background
Half-open sockets look healthy while delivering nothing. A missed-heartbeat threshold is the only reliable signal that a reconnect is needed.
Problem
Given heartbeat interval, consecutive missed pongs and a threshold, decide whether to keep the connection, send another ping, or reconnect.
Input
JSON object: { heartbeatIntervalMs, missedPongs, threshold }Output
JSON object: { action, waitMs }Sample tests
Sample 1
Input: {"heartbeatIntervalMs":30000,"missedPongs":1,"threshold":2}Expected: {"action":"ping-again","waitMs":30000}Sample 2
Input: {"heartbeatIntervalMs":30000,"missedPongs":2,"threshold":2}Expected: {"action":"reconnect","waitMs":0}Your solution
Learning objective
Detect half-open sockets instead of waiting for a user to notice.