0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ReactNative (0.54+) で HTTP/2.0 を無効にする

Last updated at Posted at 2019-11-17

ReactNative (0.54+) で、サーバサイドに AWS ALB で HTTP/2.0 を使っていた時に、HTTP/1.1 と HTTP/2.0 で挙動が変わっているっぽかったので、調査のために RN (android) からの HTTP/2.0 接続を無効にしてみました。

  • メインのReactActivity.onCreate() で OkHttpClientProvider.setOkHttpClientFactory() を呼びます。
  • OkHttpClient を返す任意のCustomClientFactoryを定義します。
[path_to_RN_proj]/android/src/main/java/your/domain/MainActivity.java
package your.domain
    :
import com.facebook.react.modules.network.OkHttpClientProvider;
    :

public class MainActivity extends ReactActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    OkHttpClientProvider.setOkHttpClientFactory(new CustomClientFactory());
    super.onCreate(savedInstanceState);
  }
    :
[path_to_RN_proj]/android/src/main/java/your/domain/CustomClientFactory.java
package your.domain

import java.util.List;
import java.util.ArrayList;

import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.ReactCookieJarContainer;

import okhttp3.OkHttpClient;
import okhttp3.Protocol;

public class CustomClientFactory implements OkHttpClientFactory {
  @Override
  public OkHttpClient createNewNetworkModuleClient() {

    List<Protocol> protocolList = new ArrayList<>();
    protocolList.add(Protocol.SPDY_3);
    protocolList.add(Protocol.HTTP_1_1);

    return OkHttpClientProvider.createClientBuilder()
      .cookieJar(new ReactCookieJarContainer())
      .protocols(protocolList)
      // .followRedirects(false)
      // .followSslRedirects(false)
      .build();
  }
}

それから react-native run android。
すると、Charlesなどで監視してみると Protocol: HTTP/1.1 に落ちている。

see also:

END

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?