Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for heartbeats in WebSocketClient #1282

Merged
merged 2 commits into from
May 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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