Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 46
feat: OkHttp implementation for making HTTP calls and WebSocket connections#1035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
30b7385d685ae73cdc8a13aaeaca1d04fc4File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| package io.ably.lib.transport; | ||
| import io.ably.lib.http.HttpUtils; | ||
| import io.ably.lib.network.EngineType; | ||
| import io.ably.lib.network.NotConnectedException; | ||
| import io.ably.lib.network.WebSocketClient; | ||
| import io.ably.lib.network.WebSocketEngine; | ||
| import io.ably.lib.network.WebSocketEngineConfig; | ||
| import io.ably.lib.network.WebSocketEngineFactory; | ||
| import io.ably.lib.network.WebSocketListener; | ||
| import io.ably.lib.network.NotConnectedException; | ||
| import io.ably.lib.types.AblyException; | ||
| import io.ably.lib.types.ErrorInfo; | ||
| import io.ably.lib.types.Param; | ||
| @@ -17,6 +18,8 @@ | ||
| import javax.net.ssl.SSLContext; | ||
| import java.nio.ByteBuffer; | ||
| import java.security.KeyManagementException; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.util.Timer; | ||
| import java.util.TimerTask; | ||
| @@ -48,16 +51,43 @@ public class WebSocketTransport implements ITransport { | ||
| private String wsUri; | ||
| private ConnectListener connectListener; | ||
| private WebSocketClient webSocketClient; | ||
| private final WebSocketEngine webSocketEngine; | ||
| private boolean activityCheckTurnedOff = false; | ||
| /****************** | ||
| * protected constructor | ||
| ******************/ | ||
| protected WebSocketTransport(TransportParams params, ConnectionManager connectionManager) { | ||
| this.params = params; | ||
| this.connectionManager = connectionManager; | ||
| this.channelBinaryMode = params.options.useBinaryProtocol; | ||
| /* We do not require Ably heartbeats, as we can use WebSocket pings instead. */ | ||
| params.heartbeats = false; | ||
| this.webSocketEngine = createWebSocketEngine(params); | ||
| params.heartbeats = !this.webSocketEngine.isPingListenerSupported(); | ||
| } | ||
| private static WebSocketEngine createWebSocketEngine(TransportParams params) { | ||
| WebSocketEngineFactory engineFactory = WebSocketEngineFactory.getFirstAvailable(); | ||
| Log.v(TAG, String.format("Using %s WebSocket Engine", engineFactory.getEngineType().name())); | ||
| WebSocketEngineConfig.WebSocketEngineConfigBuilder configBuilder = WebSocketEngineConfig.builder(); | ||
| configBuilder | ||
| .tls(params.options.tls) | ||
| .host(params.host) | ||
| .proxy(ClientOptionsUtils.convertToProxyConfig(params.getClientOptions())); | ||
| // OkHttp supports modern TLS algorithms by default | ||
| if (params.options.tls && engineFactory.getEngineType() != EngineType.OKHTTP) { | ||
| try { | ||
| SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
| sslContext.init(null, null, null); | ||
| SafeSSLSocketFactory factory = new SafeSSLSocketFactory(sslContext.getSocketFactory()); | ||
| configBuilder.sslSocketFactory(factory); | ||
| } catch (NoSuchAlgorithmException | KeyManagementException e) { | ||
| throw new IllegalStateException("Can't get safe tls algorithms", e); | ||
| } | ||
| } | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return engineFactory.create(configBuilder.build()); | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| /****************** | ||
| @@ -78,24 +108,7 @@ public void connect(ConnectListener connectListener) { | ||
| Log.d(TAG, "connect(); wsUri = " + wsUri); | ||
| synchronized (this) { | ||
| WebSocketEngineFactory engineFactory = WebSocketEngineFactory.getFirstAvailable(); | ||
| Log.v(TAG, String.format("Using %s WebSocket Engine", engineFactory.getEngineType().name())); | ||
| WebSocketEngineConfig.WebSocketEngineConfigBuilder configBuilder = WebSocketEngineConfig.builder(); | ||
| configBuilder | ||
| .tls(isTls) | ||
| .host(params.host) | ||
| .proxy(ClientOptionsUtils.convertToProxyConfig(params.getClientOptions())); | ||
| if (isTls) { | ||
| SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
| sslContext.init(null, null, null); | ||
| SafeSSLSocketFactory factory = new SafeSSLSocketFactory(sslContext.getSocketFactory()); | ||
| configBuilder.sslSocketFactory(factory); | ||
| } | ||
| WebSocketEngine engine = engineFactory.create(configBuilder.build()); | ||
| webSocketClient = engine.create(wsUri, new WebSocketHandler(this::receive)); | ||
| webSocketClient = this.webSocketEngine.create(wsUri, new WebSocketHandler(this::receive)); | ||
ttypic marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| webSocketClient.connect(); | ||
| } catch (AblyException e) { | ||
| @@ -161,6 +174,16 @@ protected void preProcessReceivedMessage(ProtocolMessage message) { | ||
| //Gives the chance to child classes to do message pre-processing | ||
| } | ||
| /** | ||
| * Visible For Testing | ||
| * </p> | ||
| * We need to turn off activity check for some tests (e.g. io.ably.lib.test.realtime.RealtimeConnectFailTest.disconnect_retry_channel_timeout_jitter_after_consistent_detach[binary_protocol]) | ||
| * Those tests expects that activity checks are passing, but protocol messages are not coming | ||
| */ | ||
| protected void turnOffActivityCheckIfPingListenerIsNotSupported() { | ||
| if (!webSocketEngine.isPingListenerSupported()) activityCheckTurnedOff = true; | ||
| } | ||
| public String toString() { | ||
| return WebSocketTransport.class.getName() + " {" + getURL() + "}"; | ||
| } | ||
| @@ -307,7 +330,7 @@ private synchronized void dispose() { | ||
| private synchronized void flagActivity() { | ||
| lastActivityTime = System.currentTimeMillis(); | ||
| connectionManager.setLastActivity(lastActivityTime); | ||
| if (activityTimerTask == null && connectionManager.maxIdleInterval != 0) { | ||
| if (activityTimerTask == null && connectionManager.maxIdleInterval != 0 && !activityCheckTurnedOff) { | ||
| /* No timer currently running because previously there was no | ||
| * maxIdleInterval configured, but now there is a | ||
| * maxIdleInterval configured. Call checkActivity so a timer | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| plugins { | ||
| `java-library` | ||
| alias(libs.plugins.lombok) | ||
| alias(libs.plugins.maven.publish) | ||
| } | ||
| java { | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| POM_ARTIFACT_ID=network-client-core | ||
| POM_NAME=Core HTTP client abstraction | ||
| POM_DESCRIPTION=Core HTTP client abstraction | ||
| POM_PACKAGING=jar |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| plugins { | ||
| `java-library` | ||
| alias(libs.plugins.lombok) | ||
| alias(libs.plugins.maven.publish) | ||
| } | ||
| java { | ||
| sourceCompatibility = JavaVersion.VERSION_1_8 | ||
| targetCompatibility = JavaVersion.VERSION_1_8 | ||
| } | ||
| dependencies { | ||
| implementation(project(":network-client-core")) | ||
| implementation(libs.okhttp) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| POM_ARTIFACT_ID=network-client-okhttp | ||
| POM_NAME=Default HTTP client | ||
| POM_DESCRIPTION=Default implementation for HTTP client | ||
| POM_PACKAGING=jar |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.