2017-02-27 92 views
0

我想用我的原生應用程序ping一個頁面,以測試URL是否可到達。在原生反應中作出ping

我試圖用ping-痘痘,但我有一個錯誤信息,我不能找到一個解決方案

UnableToResolveError: Unable to resolve module react-native-ping-litle from /Users/tetar/Desktop/myombox_react/MY_OM_BOX/index.ios.js: Module does not exist in the module map or in these directories: 
    /Users/tetar/Desktop/myombox_react/MY_OM_BOX/node_modules 
, /Users/tetar/node_modules 

回答

0

其實我這樣做

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

if (request.status === 200) { 
console.log('success'); // just print a success on console 
} else { 
console.log('error'); // just print a error on console 
} 
}; 

request.open('GET', 'http://192.168.0.254/'); //put your adresse here 
request.send(); 

我不知道這是否是最好的辦法,但它的工作對我來說

0

它可能不是一個好主意,但你可以爲你的提取請求設置一個時間。

function checkStatus(url, timeout, callback) { 
 
    return new Promise(function(resolve,reject) { 
 
    fetch(url) 
 
    .then((data)=>{ resolve(data) }) 
 
    .catch((e)=>{ reject(e) }) 
 
    
 
    setTimeout(()=>{ 
 
     callback() 
 
     reject(throw new Error('Timeout')) 
 
    },timeout) 
 
    }) 
 
}

所以您發送超時值(10,15秒​​等),URL和回調。

希望它有幫助