Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

let KeepAliveSender detect if websocket is no longer alive - fixes #6447 (non GCM) #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -18,6 +18,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -163,21 +164,15 @@ public synchronized void sendResponse(WebSocketResponseMessage response) throws
}
}

private synchronized void sendKeepAlive() throws IOException {
if (keepAliveSender != null && client != null) {
byte[] message = WebSocketMessage.newBuilder()
.setType(WebSocketMessage.Type.REQUEST)
.setRequest(WebSocketRequestMessage.newBuilder()
.setId(System.currentTimeMillis())
.setPath("/v1/keepalive")
.setVerb("GET")
.build()).build()
.toByteArray();

if (!client.send(ByteString.of(message))) {
throw new IOException("Write failed!");
}
}
private synchronized Future<Pair<Integer, String>> sendKeepAlive() throws IOException {
if (keepAliveSender == null || client == null) throw new IOException("Can't send KeepAlive Message!");

WebSocketRequestMessage request = WebSocketRequestMessage.newBuilder()
.setId(System.currentTimeMillis())
.setPath("/v1/keepalive")
.setVerb("GET")
.build();
return sendRequest(request);
}

@Override
Expand Down Expand Up @@ -300,7 +295,16 @@ public void run() {
Thread.sleep(TimeUnit.SECONDS.toMillis(KEEPALIVE_TIMEOUT_SECONDS));

Log.w(TAG, "Sending keep alive...");
sendKeepAlive();
sendKeepAlive().get(30, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
Log.w(TAG, e);
disconnect();
if (listener != null) {
listener.onDisconnected();
}
synchronized(WebSocketConnection.this) {
WebSocketConnection.this.notifyAll();
}
} catch (Throwable e) {
Log.w(TAG, e);
}
Expand Down