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

Allow user to define keepalive time interval. #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -118,7 +118,21 @@ public InputStream retrieveAttachment(SignalServiceAttachmentPointer pointer, Fi
* @return A SignalServiceMessagePipe for receiving Signal Service messages.
*/
public SignalServiceMessagePipe createMessagePipe() {
return createMessagePipe(WebSocketConnection.DEFAULT_KEEPALIVE_TIMEOUT_SECONDS);
}

/**
* Creates a pipe for receiving SignalService messages.
*
* Callers must call {@link SignalServiceMessagePipe#shutdown()} when finished with the pipe.
*
* @param kaTimeoutSeconds The number of seconds between sending keepalive messages
*
* @return A SignalServiceMessagePipe for receiving Signal Service messages.
*/
public SignalServiceMessagePipe createMessagePipe(int kaTimeoutSeconds) {
WebSocketConnection webSocket = new WebSocketConnection(urls[0].getUrl(), urls[0].getTrustStore(), credentialsProvider, userAgent);
webSocket.setKeepAliveTimeoutSeconds(kaTimeoutSeconds);
return new SignalServiceMessagePipe(webSocket, credentialsProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@

public class WebSocketConnection extends WebSocketListener {

private static final String TAG = WebSocketConnection.class.getSimpleName();
private static final int KEEPALIVE_TIMEOUT_SECONDS = 55;
public static final int DEFAULT_KEEPALIVE_TIMEOUT_SECONDS = 55;

private static final String TAG = WebSocketConnection.class.getSimpleName();

private final LinkedList<WebSocketRequestMessage> incomingRequests = new LinkedList<>();
private final Map<Long, SettableFuture<Pair<Integer, String>>> outgoingRequests = new HashMap<>();
Expand All @@ -55,17 +56,24 @@ public class WebSocketConnection extends WebSocketListener {
private KeepAliveSender keepAliveSender;
private int attempts;
private boolean connected;
private int kaTimeoutSeconds;


public WebSocketConnection(String httpUri, TrustStore trustStore, CredentialsProvider credentialsProvider, String userAgent) {
this.trustStore = trustStore;
this.credentialsProvider = credentialsProvider;
this.userAgent = userAgent;
this.attempts = 0;
this.connected = false;
this.kaTimeoutSeconds = DEFAULT_KEEPALIVE_TIMEOUT_SECONDS;
this.wsUri = httpUri.replace("https://", "wss://")
.replace("http://", "ws://") + "/v1/websocket/?login=%s&password=%s";
}

public synchronized void setKeepAliveTimeoutSeconds(int kaTimeoutSeconds) {
this.kaTimeoutSeconds = kaTimeoutSeconds;
}

public synchronized void connect() {
Log.w(TAG, "WSC connect()...");

Expand All @@ -75,8 +83,8 @@ public synchronized void connect() {

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(socketFactory.first(), socketFactory.second())
.readTimeout(KEEPALIVE_TIMEOUT_SECONDS + 10, TimeUnit.SECONDS)
.connectTimeout(KEEPALIVE_TIMEOUT_SECONDS + 10, TimeUnit.SECONDS)
.readTimeout(kaTimeoutSeconds + 10, TimeUnit.SECONDS)
.connectTimeout(kaTimeoutSeconds + 10, TimeUnit.SECONDS)
.build();

Request.Builder requestBuilder = new Request.Builder().url(filledUri);
Expand Down Expand Up @@ -281,7 +289,7 @@ private class KeepAliveSender extends Thread {
public void run() {
while (!stop.get()) {
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(KEEPALIVE_TIMEOUT_SECONDS));
Thread.sleep(TimeUnit.SECONDS.toMillis(kaTimeoutSeconds));

Log.w(TAG, "Sending keep alive...");
sendKeepAlive();
Expand Down