简体   繁体   English

如何从 Windows 机器上的博览会服务器连接到 WSL

[英]How to connect to WSL from expo server on Windows machine

Environment环境

Windows10 home Windows10 家庭版
React Native (on windows machine, not WSL) React Native(在 Windows 机器上,而不是 WSL)
Android studio 4.0安卓工作室 4.0
Virtul Device info虚拟设备信息
Name: Nexus S API 30名称:Nexus S API 30
Target: Android 10.0+(Google APIs)目标:Android 10.0+(Google API)
CPU/ABI x86 CPU/ABI x86

in WSL2 port 3000, Rails as API server在 WSL2 端口 3000 中,Rails 作为 API 服务器

Problem问题

When I use axios from android studio to Rails server on WSL, timeout error appears and even there is no log of Rails.当我在 WSL 上使用从 android studio 到 Rails 服务器的 axios 时,出现超时错误,甚至没有 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检查WSL的IP地址

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

exec Powershell (admin), connectaddr is result of ifconfig. exec Powershell (admin),connectaddr 是 ifconfig 的结果。
now port sets 3000现在端口集 3000

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

confirm确认

netsh interface portproxy show v4tov4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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