Skip to content

Commit 09f8b4b

Browse files
flolomFrancois Lolom a558367
authored andcommitted
Accept existing websocket client as parameter
The main motivation is to make possible to the user of the library to pass its own websocket client: this way, the developer can configure and/or enable/disable additionnal features of the client (ping to keep the connection alive with OkHttp, or configure timeouts for example)
1 parent 12876e9 commit 09f8b4b

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

lib/src/main/java/ua/naiksoftware/stomp/OkHttpConnectionProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@
2525

2626
private final String mUri;
2727
private final Map<String, String> mConnectHttpHeaders;
28+
private final OkHttpClient mOkHttpClient;
2829

2930
private final List<Subscriber<? super LifecycleEvent>> mLifecycleSubscribers;
3031
private final List<Subscriber<? super String>> mMessagesSubscribers;
3132

3233
private WebSocket openedSocked;
3334

3435

35-
/* package */ OkHttpConnectionProvider(String uri, Map<String, String> connectHttpHeaders) {
36+
/* package */ OkHttpConnectionProvider(String uri, Map<String, String> connectHttpHeaders, OkHttpClient okHttpClient) {
3637
mUri = uri;
3738
mConnectHttpHeaders = connectHttpHeaders != null ? connectHttpHeaders : new HashMap<>();
3839
mLifecycleSubscribers = new ArrayList<>();
3940
mMessagesSubscribers = new ArrayList<>();
41+
mOkHttpClient = okHttpClient;
4042
}
4143

4244
@Override
@@ -67,15 +69,12 @@ private void createWebSocketConnection() {
6769
throw new IllegalStateException("Already have connection to web socket");
6870
}
6971

70-
OkHttpClient okHttpClient = new OkHttpClient.Builder()
71-
.build();
72-
7372
Request.Builder requestBuilder = new Request.Builder()
7473
.url(mUri);
7574

7675
addConnectionHeadersToBuilder(requestBuilder, mConnectHttpHeaders);
7776

78-
openedSocked = okHttpClient.newWebSocket(requestBuilder.build(),
77+
openedSocked = mOkHttpClient.newWebSocket(requestBuilder.build(),
7978
new WebSocketListener() {
8079
@Override
8180
public void onOpen(WebSocket webSocket, Response response) {

lib/src/main/java/ua/naiksoftware/stomp/Stomp.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Map;
66

7+
import okhttp3.OkHttpClient;
78
import ua.naiksoftware.stomp.client.StompClient;
89

910
/**
@@ -19,7 +20,7 @@
1920
public class Stomp {
2021

2122
public static StompClient over(Class clazz, String uri) {
22-
return over(clazz, uri, null);
23+
return over(clazz, uri, null, null);
2324
}
2425

2526
/**
@@ -30,10 +31,34 @@ public static StompClient over(Class clazz, String uri) {
3031
* @return StompClient for receiving and sending messages. Call #StompClient.connect
3132
*/
3233
public static StompClient over(Class clazz, String uri, Map<String, String> connectHttpHeaders) {
34+
return over(clazz, uri, connectHttpHeaders, null);
35+
}
36+
37+
/**
38+
* {@code webSocketClient} can accept the following type of clients:
39+
* <ul>
40+
* <li>{@code org.java_websocket.WebSocket}: cannot accept an existing client</li>
41+
* <li>{@code okhttp3.WebSocket}: can accept a non-null instance of {@code okhttp3.OkHttpClient}</li>
42+
* </ul>
43+
* @param clazz class for using as transport
44+
* @param uri URI to connect
45+
* @param connectHttpHeaders HTTP headers, will be passed with handshake query, may be null
46+
* @param webSocketClient Existing client that will be used to open the WebSocket connection, may be null to use default client
47+
* @return StompClient for receiving and sending messages. Call #StompClient.connect
48+
*/
49+
public static StompClient over(Class clazz, String uri, Map<String, String> connectHttpHeaders, Object webSocketClient) {
3350
if (clazz == WebSocket.class) {
51+
52+
if (webSocketClient != null) {
53+
throw new IllegalArgumentException("You cannot pass a webSocketClient with 'org.java_websocket.WebSocket'. use null instead.");
54+
}
55+
3456
return createStompClient(new WebSocketsConnectionProvider(uri, connectHttpHeaders));
3557
} else if (clazz == okhttp3.WebSocket.class) {
36-
return createStompClient(new OkHttpConnectionProvider(uri, connectHttpHeaders));
58+
59+
OkHttpClient okHttpClient = getOkHttpClient(webSocketClient);
60+
61+
return createStompClient(new OkHttpConnectionProvider(uri, connectHttpHeaders, okHttpClient));
3762
}
3863

3964
throw new RuntimeException("Not supported overlay transport: " + clazz.getName());
@@ -42,4 +67,17 @@ public static StompClient over(Class clazz, String uri, Map<String, String> conn
4267
private static StompClient createStompClient(ConnectionProvider connectionProvider) {
4368
return new StompClient(connectionProvider);
4469
}
70+
71+
private static OkHttpClient getOkHttpClient(Object webSocketClient) {
72+
if (webSocketClient != null) {
73+
if (webSocketClient instanceof OkHttpClient) {
74+
return (OkHttpClient) webSocketClient;
75+
} else {
76+
throw new IllegalArgumentException("You must pass a non-null instance of an 'okhttp3.OkHttpClient'. Or pass null to use a default websocket client.");
77+
}
78+
} else {
79+
// default http client
80+
return new OkHttpClient();
81+
}
82+
}
4583
}

0 commit comments

Comments
 (0)