Created
February 19, 2019 07:35
-
-
Save onurcan19772/d363145398f24062413e323f8fa8ac00 to your computer and use it in GitHub Desktop.
Javascript ping
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
<!DOCTYPE html> | |
<html lang="tr"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Test sayfası</title> | |
<script> | |
// Ping kodu başlangıcı | |
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 | |
} | |
} | |
// Ping kodu sonu | |
</script> | |
</head> | |
<body> | |
<h2>Ping testi</h2> | |
<script> | |
// Çıktıyı gösterme kodu başlangıcı | |
function showResults(str) { | |
var temp = ""; | |
temp += "<p>"+str+"</p>"; //etc. | |
var outputResult = document.createElement("div"); | |
outputResult.innerHTML = temp; | |
document.body.appendChild(outputResult); | |
} | |
// Çıktıyı gösterme kodu sonu | |
// Ping isteği kodu başlangıcı | |
var pingResult = 0; | |
var m = new Number(0); | |
ping("rock55.duckdns.org", "8080", function(m) { | |
if (m >= 1) { | |
pingResult = 1; | |
showResults("Ping sonucu: " + String(pingResult)); // Ping sonucunu gösterir | |
} else { | |
pingResult = 0; | |
showResults("Ping sonucu: " + String(pingResult)); // Ping sonucunu gösterir | |
} | |
}); | |
// Ping isteği kodu sonu | |
showResults("Geçerli sayfa: " + document.URL); // Geçerli sayfayı gösterir | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment