【问题标题】:How to Simulate Internet Connection on React Native iOS Simulator?如何在 React Native iOS 模拟器上模拟 Internet 连接?
【发布时间】:2020-09-14 07:40:44
【问题描述】:

我正在尝试在 react native ios 模拟器上模拟互联网连接状态。

我使用NetInfo npm 持续检查互联网连接状态。

const unsubscribe = NetInfo.addEventListener(async (state) => {                    
    console.log("Is isInternetReachable?", state.isInternetReachable);                                                            
});    

我尝试使用“网络链接调节器”通过在 100% 丢失和 Wi-Fi 预设配置文件之间切换来模拟互联网连接状态,但它并没有改变连接状态。

【问题讨论】:

    标签: reactjs react-native networking network-programming ios-simulator


    【解决方案1】:

    您可以尝试更改 NetInfo 的配置,以更好地控制它如何检查互联网连接

    https://github.com/react-native-community/react-native-netinfo#configure

    NetInfo.configure({
      reachabilityUrl: 'https://clients3.google.com/generate_204',
      reachabilityTest: async (response) => response.status === 204,
      reachabilityLongTimeout: 60 * 1000, // 60s
      reachabilityShortTimeout: 5 * 1000, // 5s
      reachabilityRequestTimeout: 15 * 1000, // 15s
    });
    

    重要: 请注意,调用它会阻止所有先前添加的侦听器再次被调用。最好在您的应用程序启动时调用此权限以避免出现问题。

    【讨论】:

    【解决方案2】:

    “100% 丢失”不是“Internet 无法访问” 这意味着 - 这是一个连接,但它太差了,所以 100% 的数据包都丢失了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2017-08-02
      • 2019-06-30
      • 2017-07-31
      • 1970-01-01
      • 2021-01-05
      相关资源
      最近更新 更多