简体   繁体   中英

How to connect to WSL from expo server on Windows machine

Environment

Windows10 home
React Native (on windows machine, not WSL)
Android studio 4.0
Virtul Device info
Name: Nexus S API 30
Target: Android 10.0+(Google APIs)
CPU/ABI x86

in WSL2 port 3000, Rails as API server

Problem

When I use axios from android studio to Rails server on WSL, timeout error appears and even there is no log of Rails.

const { rooms } = await createWrapper().post('/hoge/hage');
//createWrapper
import Axios from "axios";
import {camelizeKeys} from "humps";

export function createWrapper({
  isMultipart = false,
} = {}): any {
  const config: any = {
    headers: {
      Accept: "application/json",
      "Content-Type": isMultipart
        ? "multipart/form-data"
        : "application/json",
      // 'X-CSRF-Token': isCsrf ? getCsrfToken() : "",
    },
    baseURL: "http://10.0.2.2:3000",
    // ↑ ***** IPv4 address of WSL also doesnt work *****
    timeout: 300000,
    withCredentials: true,
  };

  const wrapper: any = Axios.create(config);

  wrapper.interceptors.response.use(
    (response: any): any => {
      return {
        ...response,
        data: camelizeKeys(response.data),
      };
    },
    (error: any): any => {
      if (error.code === "ECONNABORTED") {
        return Promise.reject({
          ...error,
          response: {
            data: {
              error_messages: ["timeout"],//***** RESULT HERE *****
            },
          },
        });
      } else if (!error.response) {
        return Promise.reject({
          ...error,
          response: {
            data: {
              error_messages: ["system error"],
            },
          },
        });
      }
      return Promise.reject(error);
    }
  );
  return wrapper;
}

check ip address of WSL

ifconfig | grep 'inet ' | awk '{print $2}'

exec Powershell (admin), connectaddr is result of ifconfig.
now port sets 3000

netsh interface portproxy add v4tov4 listenport=3000 connectaddr=xxx.xx.xxx.xxx connectport=3000

confirm

netsh interface portproxy show v4tov4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM