mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
- added: Websocket Module setCustomClientBuilder (#37798)
Summary: Added code previously added in the following PR: https://github.com/facebook/react-native/pull/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: https://github.com/facebook/react-native/issues/37770#issuecomment-1582476332, 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 https://github.com/facebook/react-native/issues/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: https://github.com/facebook/react-native/pull/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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
b8d60a834f
commit
0cdb9e6a52
+14
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.modules.network;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
public interface CustomClientBuilder {
|
||||
public void apply(OkHttpClient.Builder builder);
|
||||
}
|
||||
+11
-5
@@ -94,7 +94,8 @@ public final class NetworkingModule extends NativeNetworkingAndroidSpec {
|
||||
private static final int CHUNK_TIMEOUT_NS = 100 * 1000000; // 100ms
|
||||
private static final int MAX_CHUNK_SIZE_BETWEEN_FLUSHES = 8 * 1024; // 8K
|
||||
|
||||
private static @Nullable CustomClientBuilder customClientBuilder = null;
|
||||
private static @Nullable com.facebook.react.modules.network.CustomClientBuilder
|
||||
customClientBuilder = null;
|
||||
|
||||
private final OkHttpClient mClient;
|
||||
private final ForwardingCookieHandler mCookieHandler;
|
||||
@@ -163,13 +164,18 @@ public final class NetworkingModule extends NativeNetworkingAndroidSpec {
|
||||
this(context, defaultUserAgent, OkHttpClientProvider.createClient(context), null);
|
||||
}
|
||||
|
||||
public static void setCustomClientBuilder(CustomClientBuilder ccb) {
|
||||
public static void setCustomClientBuilder(
|
||||
com.facebook.react.modules.network.CustomClientBuilder ccb) {
|
||||
customClientBuilder = ccb;
|
||||
}
|
||||
|
||||
public static interface CustomClientBuilder {
|
||||
public void apply(OkHttpClient.Builder builder);
|
||||
}
|
||||
/**
|
||||
* @deprecated To be removed in a future release. See
|
||||
* https://github.com/facebook/react-native/pull/37798#pullrequestreview-1518338914
|
||||
*/
|
||||
@Deprecated
|
||||
public static interface CustomClientBuilder
|
||||
extends com.facebook.react.modules.network.CustomClientBuilder {}
|
||||
|
||||
private static void applyCustomBuilder(OkHttpClient.Builder builder) {
|
||||
if (customClientBuilder != null) {
|
||||
|
||||
+19
-3
@@ -19,6 +19,7 @@ import com.facebook.react.bridge.ReadableType;
|
||||
import com.facebook.react.bridge.WritableMap;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
import com.facebook.react.module.annotations.ReactModule;
|
||||
import com.facebook.react.modules.network.CustomClientBuilder;
|
||||
import com.facebook.react.modules.network.ForwardingCookieHandler;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -48,11 +49,23 @@ public final class WebSocketModule extends NativeWebSocketModuleSpec {
|
||||
|
||||
private ForwardingCookieHandler mCookieHandler;
|
||||
|
||||
private static @Nullable CustomClientBuilder customClientBuilder = null;
|
||||
|
||||
public WebSocketModule(ReactApplicationContext context) {
|
||||
super(context);
|
||||
mCookieHandler = new ForwardingCookieHandler(context);
|
||||
}
|
||||
|
||||
public static void setCustomClientBuilder(CustomClientBuilder ccb) {
|
||||
customClientBuilder = ccb;
|
||||
}
|
||||
|
||||
private static void applyCustomBuilder(OkHttpClient.Builder builder) {
|
||||
if (customClientBuilder != null) {
|
||||
customClientBuilder.apply(builder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
for (WebSocket socket : mWebSocketConnections.values()) {
|
||||
@@ -84,12 +97,15 @@ public final class WebSocketModule extends NativeWebSocketModuleSpec {
|
||||
@Nullable final ReadableMap options,
|
||||
final double socketID) {
|
||||
final int id = (int) socketID;
|
||||
OkHttpClient client =
|
||||
OkHttpClient.Builder okHttpBuilder =
|
||||
new OkHttpClient.Builder()
|
||||
.connectTimeout(10, TimeUnit.SECONDS)
|
||||
.writeTimeout(10, TimeUnit.SECONDS)
|
||||
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
|
||||
.build();
|
||||
.readTimeout(0, TimeUnit.MINUTES); // Disable timeouts for read
|
||||
|
||||
applyCustomBuilder(okHttpBuilder);
|
||||
|
||||
OkHttpClient client = okHttpBuilder.build();
|
||||
|
||||
Request.Builder builder = new Request.Builder().tag(id).url(url);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user