Skip to content

Commit dfd8c84

Browse files
vyndorAnton SamoilovFZambia
authored
Possibility to set custom OkHttpClient (#68)
--------- Co-authored-by: Anton Samoilov <[email protected]> Co-authored-by: Alexander Emelin <[email protected]>
1 parent f1b82c4 commit dfd8c84

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

centrifuge/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ publishing {
2323
}
2424

2525
dependencies {
26-
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
26+
api 'com.squareup.okhttp3:okhttp:4.10.0'
2727
implementation 'com.google.protobuf:protobuf-javalite:3.25.5'
2828
implementation 'net.sourceforge.streamsupport:streamsupport-minifuture:1.7.4'
2929

centrifuge/src/main/java/io/github/centrifugal/centrifuge/Client.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,12 @@ private void _connect() {
259259
this.ws.cancel();
260260
}
261261

262-
OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
262+
OkHttpClient.Builder okHttpBuilder;
263+
if (opts.getOkHttpClient() != null) {
264+
okHttpBuilder = opts.getOkHttpClient().newBuilder();
265+
} else {
266+
okHttpBuilder = new OkHttpClient.Builder();
267+
}
263268

264269
Dns dns = opts.getDns();
265270
if (dns != null) {
@@ -558,7 +563,7 @@ private ServerSubscription getServerSub(String channel) {
558563
* Create new subscription to channel with SubscriptionOptions and SubscriptionEventListener
559564
*
560565
* @param channel: to create Subscription for.
561-
* @param options: to pass SubscriptionOptions, e.g. token.
566+
* @param options: to pass SubscriptionOptions, e.g. token.
562567
* @param listener: to pass event handler.
563568
* @return Subscription.
564569
* @throws DuplicateSubscriptionException if Subscription already exists in internal registry.

centrifuge/src/main/java/io/github/centrifugal/centrifuge/Options.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.net.Proxy;
66
import java.util.Map;
77

8+
import okhttp3.OkHttpClient;
9+
810
/**
911
* Configuration for a {@link Client} instance.
1012
*/
@@ -148,8 +150,21 @@ public void setMaxServerPingDelay(int maxServerPingDelay) {
148150

149151
private int maxServerPingDelay = 10000;
150152

153+
public OkHttpClient getOkHttpClient() {
154+
return this.okHttpClient;
155+
}
156+
157+
/**
158+
* Set OkHttpClient. Can be used to pass configured custom OkHttpClient (with DNS, proxy, SSLSocketFactory etc.).
159+
*/
160+
public void setOkHttpClient(OkHttpClient okHttpClient) {
161+
this.okHttpClient = okHttpClient;
162+
}
163+
164+
private OkHttpClient okHttpClient;
165+
151166
/**
152-
* Set proxy to use.
167+
* Set proxy to use in default OkHttpClient builder. Won't be used if {@link #setOkHttpClient} was used.
153168
*/
154169
public void setProxy(Proxy proxy) {
155170
this.proxy = proxy;
@@ -165,7 +180,7 @@ public Proxy getProxy() {
165180
private String proxyPassword;
166181

167182
/**
168-
* Set proxy credentials.
183+
* Set proxy credentials for default OkHttpClient builder. Won't be used if {@link #setOkHttpClient} was used.
169184
*/
170185
public void setProxyCredentials(String login, String password) {
171186
this.proxyLogin = login;
@@ -185,7 +200,7 @@ public Dns getDns() {
185200
}
186201

187202
/**
188-
* Set custom DNS resolver..
203+
* Set custom DNS resolver for default OkHttpClient builder. Won't be used if {@link #setOkHttpClient} was used.
189204
*/
190205
public void setDns(Dns dns) {
191206
this.dns = dns;
@@ -194,14 +209,15 @@ public void setDns(Dns dns) {
194209
private Dns dns;
195210

196211
/**
197-
* Set custom SSLSocketFactory
212+
* Set custom SSLSocketFactory for default OkHttpClient builder. Won't be used if {@link #setOkHttpClient} was used.
198213
*/
199214
public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
200215
this.sslSocketFactory = sslSocketFactory;
201216
}
202217

203218
/**
204-
* Set custom SSLSocketFactory and X509TrustManager
219+
* Set custom SSLSocketFactory and X509TrustManager for default OkHttpClient builder.
220+
* Won't be used if {@link #setOkHttpClient} was used.
205221
*/
206222
public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager) {
207223
this.sslSocketFactory = sslSocketFactory;

0 commit comments

Comments
 (0)