mirror of
https://github.com/oxen-io/session-android.git
synced 2025-04-30 09:20:53 +00:00
Set websocket read timeout to keepalive interaval + 10s.
// FREEBIE
This commit is contained in:
parent
e02aea9cfb
commit
25a38b9eea
@ -48,13 +48,13 @@ public class OkHttpClientWrapper implements WebSocketListener {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
public void connect(final int timeout, final TimeUnit timeUnit) {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
int attempt = 0;
|
||||
|
||||
while ((webSocket = newSocket()) != null) {
|
||||
while ((webSocket = newSocket(timeout, timeUnit)) != null) {
|
||||
try {
|
||||
Response response = webSocket.connect(OkHttpClientWrapper.this);
|
||||
|
||||
@ -117,14 +117,17 @@ public class OkHttpClientWrapper implements WebSocketListener {
|
||||
listener.onClose();
|
||||
}
|
||||
|
||||
private synchronized WebSocket newSocket() {
|
||||
private synchronized WebSocket newSocket(int timeout, TimeUnit unit) {
|
||||
if (closed) return null;
|
||||
|
||||
String filledUri = String.format(uri, credentialsProvider.getUser(), credentialsProvider.getPassword());
|
||||
SSLSocketFactory socketFactory = createTlsSocketFactory(trustStore);
|
||||
String filledUri = String.format(uri, credentialsProvider.getUser(), credentialsProvider.getPassword());
|
||||
OkHttpClient okHttpClient = new OkHttpClient();
|
||||
|
||||
return WebSocket.newWebSocket(new OkHttpClient().setSslSocketFactory(socketFactory),
|
||||
new Request.Builder().url(filledUri).build());
|
||||
okHttpClient.setSslSocketFactory(createTlsSocketFactory(trustStore));
|
||||
okHttpClient.setReadTimeout(timeout, unit);
|
||||
okHttpClient.setConnectTimeout(timeout, unit);
|
||||
|
||||
return WebSocket.newWebSocket(okHttpClient, new Request.Builder().url(filledUri).build());
|
||||
}
|
||||
|
||||
private SSLSocketFactory createTlsSocketFactory(TrustStore trustStore) {
|
||||
|
@ -19,7 +19,8 @@ import static org.whispersystems.textsecure.internal.websocket.WebSocketProtos.W
|
||||
|
||||
public class WebSocketConnection implements WebSocketEventListener {
|
||||
|
||||
private static final String TAG = WebSocketConnection.class.getSimpleName();
|
||||
private static final String TAG = WebSocketConnection.class.getSimpleName();
|
||||
private static final int KEEPALIVE_TIMEOUT_SECONDS = 55;
|
||||
|
||||
private final LinkedList<WebSocketRequestMessage> incomingRequests = new LinkedList<>();
|
||||
|
||||
@ -42,7 +43,7 @@ public class WebSocketConnection implements WebSocketEventListener {
|
||||
|
||||
if (client == null) {
|
||||
client = new OkHttpClientWrapper(wsUri, trustStore, credentialsProvider, this);
|
||||
client.connect();
|
||||
client.connect(KEEPALIVE_TIMEOUT_SECONDS + 10, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,6 +141,7 @@ public class WebSocketConnection implements WebSocketEventListener {
|
||||
|
||||
public synchronized void onConnected() {
|
||||
if (client != null && keepAliveSender == null) {
|
||||
Log.w(TAG, "onConnected()");
|
||||
keepAliveSender = new KeepAliveSender();
|
||||
keepAliveSender.start();
|
||||
}
|
||||
@ -156,7 +158,7 @@ public class WebSocketConnection implements WebSocketEventListener {
|
||||
public void run() {
|
||||
while (!stop.get()) {
|
||||
try {
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(55));
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(KEEPALIVE_TIMEOUT_SECONDS));
|
||||
|
||||
Log.w(TAG, "Sending keep alive...");
|
||||
sendKeepAlive();
|
||||
|
Loading…
x
Reference in New Issue
Block a user