Reactjs 平在本地

Reactjs 平在本地,reactjs,react-native,ping,Reactjs,React Native,Ping,我想在我的项目中做出一个ping。我已经尝试了ping litle库,但它不起作用。我也试过: var request = new xhtmlrequest(); request.onreadystatechange = (e) => { if (request.readyState !== 4) { return; } if (request.status === 200) { console.log('success');

我想在我的项目中做出一个ping。我已经尝试了
ping litle
库,但它不起作用。我也试过:

var request = new xhtmlrequest();
request.onreadystatechange = (e) => {
    if (request.readyState !== 4) {
        return;
    }

    if (request.status === 200) {
         console.log('success'); 
    } else {
         console.log('error'); 
    }
 };

request.open('GET', 'http://192.168.0.254/'); 
request.send();
但是,当我第二次调用该函数时,即使我的主机断开连接,也会得到相同的结果。 你有没有一个好主意,使一个良好的平在中国? 或者如何销毁我的
xhtmlrequest

使用react native提供的

您的代码如下所示:

fetch('http://192.168.0.254')
  .then((response) => {
     if (response.status === 200) {
       console.log('success');
     } else {
       console.log('error');
     }
   })
  .catch((error) => {
       console.log('network error: ' + error);
   })

您可能想编写:
newXMLHttpRequest()
这将基本上检查远程IP的端口80上是否有正在运行的http服务器,而不是PING。计算机可以ping而不需要服务器端口80上的http服务器,也可以在运行http服务器时忽略ping查询。最好的例子是4.2.2.2 DNS服务器,通常用于检查您是否可以访问internet。如果你可以PING它(通过PING查询),你可以告诉你已经访问了互联网,但是这个服务器不会响应端口80上的http请求。现在回答你的问题,有一个react原生PING插件,即使我觉得它不是很稳定。