|
18 | 18 | */ |
19 | 19 | public class LiveEventSubscriber { |
20 | 20 |
|
21 | | - private static final long WEBSOCKET_PING_INTERVAL = 60000; // 60 seconds |
22 | 21 | private static final long RECONNECT_INTERVAL_BASE = 4000; // 4 seconds |
23 | 22 |
|
24 | 23 | private static final Gson gson = new Gson(); |
25 | 24 | private final OkHttpClient client = new OkHttpClient.Builder() |
26 | 25 | .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 |
28 | 28 | .connectionPool(new ConnectionPool(5, 5, TimeUnit.MINUTES)) // create our own connection pool so it doesn't get shared w/ another okhttpclient config |
29 | 29 | .build(); |
30 | 30 |
|
@@ -135,7 +135,6 @@ private class FastCommentsWebSocketListener extends WebSocketListener { |
135 | 135 | private final CanSeeCommentsCallback canSeeCommentsCallback; |
136 | 136 | private final boolean[] isIntentionallyClosed; |
137 | 137 | private final CommentWidgetConfig config; |
138 | | - private final Timer pingTimer = new Timer(); |
139 | 138 | private Timer reconnectTimer; |
140 | 139 | private final String tenantId; |
141 | 140 | private final String urlId; |
@@ -166,18 +165,6 @@ public void onOpen(WebSocket webSocket, Response response) { |
166 | 165 | return; |
167 | 166 | } |
168 | 167 |
|
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 | | - |
181 | 168 | // Fetch missed events if we have a last event time |
182 | 169 | if (lastEventTime > 0) { |
183 | 170 | fetchEventLog(tenantId, urlId, userIdWS, lastEventTime, new Date().getTime(), |
@@ -248,9 +235,6 @@ public void onClosed(WebSocket webSocket, int code, String reason) { |
248 | 235 | } |
249 | 236 |
|
250 | 237 | private void cleanup() { |
251 | | - // Clean up ping timer |
252 | | - pingTimer.cancel(); |
253 | | - |
254 | 238 | // Cancel any existing reconnect timer |
255 | 239 | if (reconnectTimer != null) { |
256 | 240 | reconnectTimer.cancel(); |
|
0 commit comments