Skip to content

Commit 7c1ead1

Browse files
authored
Add experimental OkHttp3 websocket client support (#19)
We should deprecate TubeSocket if this works.
1 parent 3b0041d commit 7c1ead1

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

ParseLiveQuery/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ android {
4242
dependencies {
4343
compile 'com.parse:parse-android:1.14.0'
4444
compile 'com.firebase:tubesock:0.0.12'
45+
compile 'com.squareup.okhttp3:okhttp:3.6.0'
4546
compile 'com.parse.bolts:bolts-tasks:1.4.0'
4647

4748
testCompile 'org.robolectric:robolectric:3.0'
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.parse;
2+
3+
import android.util.Log;
4+
5+
import java.net.URI;
6+
import java.util.Locale;
7+
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.Response;
11+
import okhttp3.WebSocket;
12+
import okhttp3.WebSocketListener;
13+
import okio.ByteString;
14+
15+
/* package */ class OkHttp3WebSocketClient implements WebSocketClient {
16+
17+
private static final String LOG_TAG = "OkHttpWebSocketClient";
18+
19+
private final WebSocketClientCallback webSocketClientCallback;
20+
private WebSocket webSocket;
21+
private State state = State.NONE;
22+
private final OkHttpClient client;
23+
private final String url;
24+
private final int STATUS_CODE = 200;
25+
private final String CLOSING_MSG = "User invoked close";
26+
27+
private final WebSocketListener handler = new WebSocketListener() {
28+
@Override
29+
public void onOpen(WebSocket webSocket, Response response) {
30+
setState(State.CONNECTED);
31+
webSocketClientCallback.onOpen();
32+
}
33+
34+
@Override
35+
public void onMessage(WebSocket webSocket, String text) {
36+
webSocketClientCallback.onMessage(text);
37+
}
38+
39+
@Override
40+
public void onMessage(WebSocket webSocket, ByteString bytes) {
41+
Log.w(LOG_TAG, String.format(Locale.US, "Socket got into inconsistent state and received %s instead.", bytes.toString()));
42+
}
43+
44+
@Override
45+
public void onClosed(WebSocket webSocket, int code, String reason) {
46+
setState(State.DISCONNECTED);
47+
webSocketClientCallback.onClose();
48+
}
49+
50+
@Override
51+
public void onFailure(okhttp3.WebSocket webSocket, Throwable t, Response response) {
52+
webSocketClientCallback.onError(t);
53+
}
54+
};
55+
56+
private OkHttp3WebSocketClient(OkHttpClient okHttpClient, WebSocketClientCallback webSocketClientCallback, URI hostUrl) {
57+
client = okHttpClient;
58+
this.webSocketClientCallback = webSocketClientCallback;
59+
url = hostUrl.toString();
60+
}
61+
62+
@Override
63+
public synchronized void open() {
64+
if (State.NONE == state) {
65+
// OkHttp3 connects as soon as the socket is created so do it here.
66+
Request request = new Request.Builder()
67+
.url(url)
68+
.build();
69+
70+
webSocket = client.newWebSocket(request, handler);
71+
setState(State.CONNECTING);
72+
}
73+
}
74+
75+
@Override
76+
public synchronized void close() {
77+
setState(State.DISCONNECTING);
78+
webSocket.close(STATUS_CODE, CLOSING_MSG);
79+
}
80+
81+
@Override
82+
public void send(String message) {
83+
if (state == State.CONNECTED) {
84+
webSocket.send(message);
85+
}
86+
}
87+
88+
@Override
89+
public State getState() {
90+
return state;
91+
}
92+
93+
private synchronized void setState(State newState) {
94+
this.state = newState;
95+
this.webSocketClientCallback.stateChanged();
96+
}
97+
98+
/* package */ static class OkHttp3SocketClientFactory implements WebSocketClientFactory {
99+
@Override
100+
public WebSocketClient createInstance(WebSocketClientCallback webSocketClientCallback, URI hostUrl) {
101+
return new OkHttp3WebSocketClient(new OkHttpClient(), webSocketClientCallback, hostUrl);
102+
}
103+
104+
public WebSocketClient createInstance(OkHttpClient client, WebSocketClientCallback webSocketClientCallback, URI hostUrl) {
105+
return new OkHttp3WebSocketClient(client, webSocketClientCallback, hostUrl);
106+
}
107+
108+
}
109+
110+
}

ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public void onClose() {
311311
}
312312

313313
@Override
314-
public void onError(Exception exception) {
314+
public void onError(Throwable exception) {
315315
Log.e(LOG_TAG, "Socket onError", exception);
316316
}
317317

ParseLiveQuery/src/main/java/com/parse/WebSocketClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface WebSocketClientCallback {
1717

1818
void onClose();
1919

20-
void onError(Exception exception);
20+
void onError(Throwable exception);
2121

2222
void stateChanged();
2323
}

0 commit comments

Comments
 (0)