Skip to content

Commit

Permalink
Add Websocket Module setCustomClientBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
DDRBoxman committed Feb 15, 2024
1 parent 2d0c1cf commit 65ee814
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public interface ResponseHandler {
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;
Expand Down Expand Up @@ -163,13 +164,18 @@ public NetworkingModule(ReactApplicationContext context, String defaultUserAgent
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -55,11 +56,23 @@ public interface OnOpenHandler {

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()) {
Expand Down Expand Up @@ -91,12 +104,15 @@ public void connect(
@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);

Expand Down

0 comments on commit 65ee814

Please sign in to comment.