[英]How to connect to WSL from expo server on Windows machine
Windows10 家庭版
React Native(在 Windows 機器上,而不是 WSL)
安卓工作室 4.0
虛擬設備信息
名稱:Nexus S API 30
目標:Android 10.0+(Google API)
CPU/ABI x86
在 WSL2 端口 3000 中,Rails 作為 API 服務器
當我在 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;
}
檢查WSL的IP地址
ifconfig | grep 'inet ' | awk '{print $2}'
exec Powershell (admin),connectaddr 是 ifconfig 的結果。
現在端口集 3000
netsh interface portproxy add v4tov4 listenport=3000 connectaddr=xxx.xx.xxx.xxx connectport=3000
確認
netsh interface portproxy show v4tov4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.