Skip to content

Commit 0cdb9e6

Browse files
MO-Lewisfacebook-github-bot
authored andcommitted
- added: Websocket Module setCustomClientBuilder (#37798)
Summary: Added code previously added in the following PR: #28659. The above PR was accidentally scrapped due to the renaming of the master branch on the React Native repo. As advised in this comment: #37770 (comment), I'm opening a new PR with the same code to get this merged into master / main. Currently, we need to run a local fork of React Native and manually apply these changes ourselves. This then causes additional issues, as it's currently _**impossible**_ to build React Native from source when running on a Windows machine, as evidenced in #37770 and the other linked issues nested inside of this issue. **Original summary is as follows:** With `NetworkModule.setCustomClientBuilder` we can customize our OkHttpClient to all requests made by react-native, it's very useful when you do `SSL Pinning` or change some OkHttpClient configuration at all. I've added a similar function to websocket, it allow us do some configurations on Websocket OkHttpClient. ## Changelog: [Android] [Added] - Websocket Module setCustomClientBuilder <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests Pull Request resolved: #37798 Test Plan: **From the original PR:** You can just set a custom `CustomClientBuilder` on `MainActivity` `onCreate`: ``` import okhttp3.OkHttpClient; import java.util.concurrent.TimeUnit; import com.facebook.react.modules.network.CustomClientBuilder; import com.facebook.react.modules.websocket.WebsocketModule; public class MainActivity extends ReactFragmentActivity { Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); RNBootSplash.init(R.drawable.launch_screen, MainActivity.this); WebsocketModule.setCustomClientBuilder(new CustomClientBuilder() { Override public void apply(OkHttpClient.Builder builder) { builder.connectTimeout(0, TimeUnit.MILLISECONDS); } }); } ... ``` Reviewed By: cortinico Differential Revision: D47468613 Pulled By: javache fbshipit-source-id: ad97fb18ba5784d8abe157f5ccd29201b8b0fe84
1 parent b8d60a8 commit 0cdb9e6

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.modules.network;
9+
10+
import okhttp3.OkHttpClient;
11+
12+
public interface CustomClientBuilder {
13+
public void apply(OkHttpClient.Builder builder);
14+
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ public interface ResponseHandler {
9494
private static final int CHUNK_TIMEOUT_NS = 100 * 1000000; // 100ms
9595
private static final int MAX_CHUNK_SIZE_BETWEEN_FLUSHES = 8 * 1024; // 8K
9696

97-
private static @Nullable CustomClientBuilder customClientBuilder = null;
97+
private static @Nullable com.facebook.react.modules.network.CustomClientBuilder
98+
customClientBuilder = null;
9899

99100
private final OkHttpClient mClient;
100101
private final ForwardingCookieHandler mCookieHandler;
@@ -163,13 +164,18 @@ public NetworkingModule(ReactApplicationContext context, String defaultUserAgent
163164
this(context, defaultUserAgent, OkHttpClientProvider.createClient(context), null);
164165
}
165166

166-
public static void setCustomClientBuilder(CustomClientBuilder ccb) {
167+
public static void setCustomClientBuilder(
168+
com.facebook.react.modules.network.CustomClientBuilder ccb) {
167169
customClientBuilder = ccb;
168170
}
169171

170-
public static interface CustomClientBuilder {
171-
public void apply(OkHttpClient.Builder builder);
172-
}
172+
/**
173+
* @deprecated To be removed in a future release. See
174+
* https://github.com/facebook/react-native/pull/37798#pullrequestreview-1518338914
175+
*/
176+
@Deprecated
177+
public static interface CustomClientBuilder
178+
extends com.facebook.react.modules.network.CustomClientBuilder {}
173179

174180
private static void applyCustomBuilder(OkHttpClient.Builder builder) {
175181
if (customClientBuilder != null) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.facebook.react.bridge.WritableMap;
2020
import com.facebook.react.common.ReactConstants;
2121
import com.facebook.react.module.annotations.ReactModule;
22+
import com.facebook.react.modules.network.CustomClientBuilder;
2223
import com.facebook.react.modules.network.ForwardingCookieHandler;
2324
import java.io.IOException;
2425
import java.net.URI;
@@ -48,11 +49,23 @@ public interface ContentHandler {
4849

4950
private ForwardingCookieHandler mCookieHandler;
5051

52+
private static @Nullable CustomClientBuilder customClientBuilder = null;
53+
5154
public WebSocketModule(ReactApplicationContext context) {
5255
super(context);
5356
mCookieHandler = new ForwardingCookieHandler(context);
5457
}
5558

59+
public static void setCustomClientBuilder(CustomClientBuilder ccb) {
60+
customClientBuilder = ccb;
61+
}
62+
63+
private static void applyCustomBuilder(OkHttpClient.Builder builder) {
64+
if (customClientBuilder != null) {
65+
customClientBuilder.apply(builder);
66+
}
67+
}
68+
5669
@Override
5770
public void invalidate() {
5871
for (WebSocket socket : mWebSocketConnections.values()) {
@@ -84,12 +97,15 @@ public void connect(
8497
@Nullable final ReadableMap options,
8598
final double socketID) {
8699
final int id = (int) socketID;
87-
OkHttpClient client =
100+
OkHttpClient.Builder okHttpBuilder =
88101
new OkHttpClient.Builder()
89102
.connectTimeout(10, TimeUnit.SECONDS)
90103
.writeTimeout(10, TimeUnit.SECONDS)
91-
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
92-
.build();
104+
.readTimeout(0, TimeUnit.MINUTES); // Disable timeouts for read
105+
106+
applyCustomBuilder(okHttpBuilder);
107+
108+
OkHttpClient client = okHttpBuilder.build();
93109

94110
Request.Builder builder = new Request.Builder().tag(id).url(url);
95111

0 commit comments

Comments
 (0)