简体   繁体   English

检查与IP地址的连接是否丢失

[英]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.comhttps://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,它将以简单的“确定”或“丢失”返回连接状态。

By ping I mean ICMP ping ping是指ICMP ping

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何在不与服务器建立任何连接的情况下检查我们的IP地址? - How can we check our IP Address without making any connection to server? 通过 ip 地址连接到 websocket 被 firefox 拒绝 - Connection to websocket via ip address refused by firefox 如何在jquery中检查IP地址的验证 - How to check validation of IP Address in jquery 如何检查访问者的IP地址是否仍处于活动状态或在线状态? - How to check if visitor's IP Address is still active or online? 如何检查用户的IP地址并在我的网页中显示不同的内容 - How to check ip address of an user and show different content in my webpage 仅使用javascript规则表达式检查IP地址范围 - Check range of IP address using javascript reqular expression only 在JScript中使用服务器IP和端口号检查telnet连接 - Check telnet connection with server IP and Port number in JScript Socket.io 给出相同的 ip 地址,无论从何处发生连接(使用 repl.it 托管) - Socket.io gives same ip address no matter where the connection is happening from (hosting with repl.it) 对于javascript中的websocket连接,如果想与其他设备通信,我如何获得可以通过websocket连接传递的IP地址? - For websocket connection in javascript if wants to communicate to other device how I can get IP address which I can pass through websocket connection? IP 地址记录和比较 - IP address logs and compare
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM