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