Skip to content

Commit 860edec

Browse files
committed
ping and keepalive adjustments
1 parent 1d0cc99 commit 860edec

1 file changed

Lines changed: 2 additions & 18 deletions

File tree

pubsub/src/main/java/com/fastcomments/pubsub/LiveEventSubscriber.java

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
*/
1919
public class LiveEventSubscriber {
2020

21-
private static final long WEBSOCKET_PING_INTERVAL = 60000; // 60 seconds
2221
private static final long RECONNECT_INTERVAL_BASE = 4000; // 4 seconds
2322

2423
private static final Gson gson = new Gson();
2524
private final OkHttpClient client = new OkHttpClient.Builder()
2625
.readTimeout(0, TimeUnit.SECONDS) // no read timeout for ws
27-
.protocols(Collections.singletonList(Protocol.HTTP_1_1)) // having trouble w/ http/2 in emulators
26+
.pingInterval(25, TimeUnit.SECONDS) // protocol-level WebSocket ping to keep NATs alive (emulators, silly networks, etc)
27+
.protocols(Collections.singletonList(Protocol.HTTP_1_1)) // just skip negotiating, server is 1.1, reduces latency
2828
.connectionPool(new ConnectionPool(5, 5, TimeUnit.MINUTES)) // create our own connection pool so it doesn't get shared w/ another okhttpclient config
2929
.build();
3030

@@ -135,7 +135,6 @@ private class FastCommentsWebSocketListener extends WebSocketListener {
135135
private final CanSeeCommentsCallback canSeeCommentsCallback;
136136
private final boolean[] isIntentionallyClosed;
137137
private final CommentWidgetConfig config;
138-
private final Timer pingTimer = new Timer();
139138
private Timer reconnectTimer;
140139
private final String tenantId;
141140
private final String urlId;
@@ -166,18 +165,6 @@ public void onOpen(WebSocket webSocket, Response response) {
166165
return;
167166
}
168167

169-
// Setup ping timer to keep connection alive
170-
pingTimer.scheduleAtFixedRate(new TimerTask() {
171-
@Override
172-
public void run() {
173-
try {
174-
webSocket.send("ping");
175-
} catch (Exception e) {
176-
// Ignore ping errors
177-
}
178-
}
179-
}, WEBSOCKET_PING_INTERVAL, WEBSOCKET_PING_INTERVAL);
180-
181168
// Fetch missed events if we have a last event time
182169
if (lastEventTime > 0) {
183170
fetchEventLog(tenantId, urlId, userIdWS, lastEventTime, new Date().getTime(),
@@ -248,9 +235,6 @@ public void onClosed(WebSocket webSocket, int code, String reason) {
248235
}
249236

250237
private void cleanup() {
251-
// Clean up ping timer
252-
pingTimer.cancel();
253-
254238
// Cancel any existing reconnect timer
255239
if (reconnectTimer != null) {
256240
reconnectTimer.cancel();

0 commit comments

Comments
 (0)