Skip to content

Commit

Permalink
Add check for heartbeats in WebSocketClient (#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinnDevelopment committed May 2, 2020
1 parent bad9bed commit 6ba908c
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class WebSocketClient extends WebSocketAdapter implements WebSocketListen

protected boolean initiating;

protected int missedHeartbeats = 0;
protected int reconnectTimeoutS = 2;
protected long heartbeatStartTime;
protected long identifyTime = 0;
Expand Down Expand Up @@ -616,8 +617,18 @@ protected void sendKeepAlive()
.put("d", api.getResponseTotal()
).toString();

send(keepAlivePacket, true);
heartbeatStartTime = System.currentTimeMillis();
if (missedHeartbeats >= 2)
{
missedHeartbeats = 0;
LOG.warn("Missed 2 heartbeats! Trying to reconnect...");
close(4900, "ZOMBIE CONNECTION");
}
else
{
missedHeartbeats += 1;
send(keepAlivePacket, true);
heartbeatStartTime = System.currentTimeMillis();
}
}

protected void sendIdentify()
Expand Down Expand Up @@ -803,6 +814,7 @@ protected void onEvent(DataObject content)
break;
case WebSocketCode.HEARTBEAT_ACK:
LOG.trace("Got Heartbeat Ack (OP 11).");
missedHeartbeats = 0;
api.setGatewayPing(System.currentTimeMillis() - heartbeatStartTime);
break;
default:
Expand Down

0 comments on commit 6ba908c

Please sign in to comment.