[英]Check if connection to IP address is lost
For my web app I need a way to detect whether the application has lost the connection to the IP address (100.100.100.100) it runs on or whether the device has connected to a different network/connection. 对于我的Web应用程序,我需要一种方法来检测该应用程序是否已失去与运行该应用程序的IP地址(100.100.100.100)的连接,或者该设备是否已连接至其他网络/连接。 This must all be done client side. 这必须全部在客户端完成。 Currently I am doing the following but it doesn't seem to work. 目前,我正在执行以下操作,但似乎无法正常工作。 Any ideas how to accomplish this? 任何想法如何做到这一点? Thanks. 谢谢。
$.ajax({
type: "GET",
url: "100.100.100.100",
success: function(){
alert("Connection Good!")
}
error: function(XMLHttpRequest, textStatus, errorThrown){
if(textStatus == 'timeout') {
alert("Connection has been lost.");
}
}
});
It's not going to work to a random IP address due to the same-origin policy. 由于同源策略,它无法用于随机IP地址。 The best the thing to do is to have a request to a URL on the server your app is served from (ie same Authority as the JS file comes from) which gives a known good response. 最好的办法是向应用服务的服务器上的URL请求(即与JS文件来自的授权机构相同),从而给出良好的响应。
** Update Just to be clear, by 'Authority' I really mean host, port, and protocol. **更新为了明确起见,“权限”实际上是指主机,端口和协议。 So for example, http://google.com is not the same as https://google.com nor is it the same as http://google.com:8080 因此,例如, http://google.com与https://google.com不同,也与http://google.com:8080不同
You can create simple PHP (or whatever language you are using) which actually pings the target IP. 您可以创建简单的PHP(或您使用的任何语言)来实际ping目标IP。
Then you can call 那你可以打电话
http://www.example.com/my_ping_script.php?target=10.10.10.10
through AJAX and it will return the connection status as simple "OK" or "LOST". 通过AJAX,它将以简单的“确定”或“丢失”返回连接状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.