Last active
February 13, 2023 14:46
-
-
Save rodrigobdz/f9969f9e49cf24ef635db4ee90bf3459 to your computer and use it in GitHub Desktop.
Ping IP Address in React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('http://localhost') | |
.then((response) => { | |
console.log('response ', response) | |
if (response.status === 200) { | |
console.log('success') | |
} else { | |
console.warn('error') | |
} | |
}) | |
.catch((error) => { | |
console.error('network error: ' + error) | |
}) |
This is not actually ping, this will send a http request into the server and wait for its response.
This approach will cost more resource and time than ping.
What would you recommend instead?
What about a lightweight solution? It seems oversized to build an app to ping a server.
@rodrigobdz you are assuming web server is running in that host, it might be just ftp,dns,etc. In those cases fetch won't work
@rodrigobdz you are assuming web server is running in that host, it might be just ftp,dns,etc. In those cases fetch won't work
@veera83372 Thanks for the heads-up
just a heads up. It doesn't work when you try to ping (fetch) an http
URL from a https
host.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ping IP Address in React Native
Tests if a host is reachable from a React Native application.
Requirements
Usage
Import and run ping.js in a React Native app.
Credits
Adapted from https://stackoverflow.com/a/43345642/2227405