Created
October 19, 2017 02:48
-
-
Save aloha1003/3b57189e876a681bbe36834b0d77a5c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function ping(host, port, pong) { | |
var started = new Date().getTime(); | |
var http = new XMLHttpRequest(); | |
http.open("GET", "http://" + host + ":" + port, /*async*/true); | |
http.onreadystatechange = function() { | |
if (http.readyState == 4) { | |
var ended = new Date().getTime(); | |
var milliseconds = ended - started; | |
if (pong != null) { | |
pong(milliseconds); | |
} | |
} | |
}; | |
try { | |
http.send(null); | |
} catch(exception) { | |
// this is expected | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment