你的位置:首页 > 互联网IT
本地ping被限制,html代码本地ping,在线ping代码,ping被限制,怎么在线ping,网页ping代码
高防服务器是不可以往外ping,所以用http代码ping别的IP测试丢包与延迟
在线演示地址:演示地址打开ping.html
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTTP Ping</title>
<style>
html {
height: 100%;
overflow: hidden;
}
body {
background: #000;
color: #C0C0C0;
font-weight: bold;
font-size: 14px;
font-family: Lucida Console;
height: 100%;
margin: 0 0 0 5px;
}
#divContent {
height: 90%;
overflow: auto;
}
#txtTimeout {
width: 40px;
}
button {
margin-left: 10px;
}
</style>
</head>
<body>
<div id="divInput">
<span>URL:</span>
<input id="txtURL" type="text" />
<span>Timeout:</span>
<input id="txtTimeout" type="text" value="2000" />
<input id="btnSwitch" type="button" value="Start" onclick="handleBtnClick()" />
<hr />
</div>
<div id="divContent"></div>
<script>
var intStartTime;
var objIMG = new Image();
objIMG.onload = objIMG.onerror = function() {
/*
* ԐܘӦ,ȡлӬʱ݆ʱ
*/
clearTimeout(intTimerID);
if (!bolIsRunning || bolIsTimeout)
return;
var delay = new Date() - intStartTime;
console.log(delay)
println("正在 ping " +
strURL +
" time" +
((delay < 1) ? ("<1") : ("=" + delay)) +
"ms");
arrDelays.push(delay);
console.log(arrDelays)
/*
* ÿՎȫȳݤٴО׆՚1īӔʏ
*/
setTimeout(ping, delay < 1000 ? (1000 - delay) : 1000);
}
function ping() {
/*
* ע̍ȫȳ
*/
intStartTime = +new Date();
intSent++;
objIMG.src = strURL + "/" + intStartTime;
bolIsTimeout = false;
/*
* Ӭʱ݆ʱ
*/
intTimerID = setTimeout(timeout, intTimeout);
}
function timeout() {
if (!bolIsRunning)
return;
bolIsTimeout = true;
objIMG.src = "X:\\";
console.log(11111, objIMG.src)
println("请求超时。");
ping();
}
</script>
<script>
var $ = function(v) {
return document.getElementById(v)
};
var arrDelays = [];
var intSent;
var bolIsRunning = false;
var bolIsTimeout;
var strURL;
var intTimeout;
var intTimerID;
var objBtn = $("btnSwitch");
var objContent = $("divContent");
var objTxtURL = $("txtURL");
objTxtURL.value = window.location.host;
function handleBtnClick() {
if (bolIsRunning) {
/*
* ֹͣ
*/
var intRecv = arrDelays.length;
var intLost = intSent - intRecv;
console.log(intRecv, intLost)
var sum = 0;
for (var i = 0; i < intRecv; i++)
sum += arrDelays[i];
objBtn.value = "Start";
bolIsRunning = false;
/*
* ͳ݆ޡڻ
*/
println("" + strURL + " ping结束");
println("统计信息 " + strURL + ":");
println(" 数据包: 已发送 = " +
intSent +
", 已接收 = " +
intRecv +
", 丢失 = " +
intLost +
" (" +
Math.floor(intLost / intSent * 100) +
"% 丢失率),");
if (intRecv == 0)
return;
println("往返行程的估计时间(以毫秒为单位):");
println("最短 = " +
Math.min.apply(this, arrDelays) +
"ms, 最长 = " +
Math.max.apply(this, arrDelays) +
"ms, 平均 = " +
Math.floor(sum / intRecv) +
"ms");
} else {
/*
* ߪʼ
*/
strURL = objTxtURL.value;
console.log(strURL)
if (strURL.length == 0)
return;
if (strURL.substring(0, 7).toLowerCase() != "http://")
strURL = "http://" + strURL;
intTimeout = parseInt($("txtTimeout").value, 10);
if (isNaN(intTimeout))
intTimeout = 2000;
if (intTimeout < 1000)
intTimeout = 1000;
objBtn.value = "Stop";
bolIsRunning = true;
arrDelays = [];
intSent = 0;
cls();
ping();
}
}
function println(str) {
var objDIV = document.createElement("div");
if (objDIV.innerText != null)
objDIV.innerText = str;
else
objDIV.textContent = str;
objContent.appendChild(objDIV);
objContent.scrollTop = objContent.scrollHeight;
}
function cls() {
objContent.innerHTML = "";
}
</script>
</body>
</html>
发表评论: