Skip to content

Commit

Permalink
Added code previously added in the following PR: facebook#28659
Browse files Browse the repository at this point in the history
  • Loading branch information
MO-Lewis committed Jun 9, 2023
1 parent 5cc8cee commit 7232fbb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 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 @@ -167,10 +167,6 @@ public static void setCustomClientBuilder(CustomClientBuilder ccb) {
customClientBuilder = ccb;
}

public static interface CustomClientBuilder {
public void apply(OkHttpClient.Builder builder);
}

private static void applyCustomBuilder(OkHttpClient.Builder builder) {
if (customClientBuilder != null) {
customClientBuilder.apply(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.facebook.react.common.ReactConstants;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.network.ForwardingCookieHandler;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.CustomClientBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -48,11 +50,23 @@ public interface ContentHandler {

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 @@ -84,12 +98,15 @@ public void connect(
@Nullable final ReadableMap options,
final double socketID) {
final int id = (int) socketID;
OkHttpClient client =
new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
.build();
OkHttpClient.Builder okHttpBuilder =
new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.modules.network.NetworkingModule;
import com.facebook.react.modules.network.CustomClientBuilder;
import okhttp3.OkHttpClient;

public class ReactNativeFlipper {
Expand All @@ -39,7 +40,7 @@ public static void initializeFlipper(Context context, ReactInstanceManager react

NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin();
NetworkingModule.setCustomClientBuilder(
new NetworkingModule.CustomClientBuilder() {
new CustomClientBuilder() {
@Override
public void apply(OkHttpClient.Builder builder) {
builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
Expand Down

0 comments on commit 7232fbb

Please sign in to comment.