I need a event to detect Internet connect/disconnect
This is all covered (including the difference between being on the network and having the network connect you to the Internet) at http://msdn.microsoft.com/en-us/library/ee264321(VS.85).aspx. I hope you meant to put that Windows 7 tag on your post, because all this is pretty new.
The key is INetworkListManager.get_IsConnectedToInternet()
which pretty much does what it says on the tin. You have to jump around a bit to register for the events etc. The Code Pack wraps some of that up for you and has a network sample you can adapt.
How to sense a network connection and trigger an event
You can register to NetworkChange.NetworkAvailabilityChanged
event for receiving events when the availability of the network changes
From MSDN:
The NetworkChange class raises NetworkAvailabilityChanged events when the availability of the network changes. The network is available when at least one network interface is marked "up" and is not a tunnel or loopback interface.
To have a NetworkChange object call an event-handling method when a NetworkAvailabilityChanged event occurs, you must associate the method with a NetworkAvailabilityChangedEventHandler delegate, and add this delegate to this event.
The NetworkAvailabilityChanged event is supported on Windows 2000 and later.
For more information
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkchange.networkavailabilitychanged.aspx
Detect internet disconnection via socket
You have to understand heartbeat interval
and heartbeat timeout
. Earlier is the time when server emits a heartbeat each 25 seconds (by default). And later is timeout for server to keep that specific client in memory. If client receives the heartbeat within heartbeat timeout
(60 seconds by default) it tells the server that I'm alive. If client doesn't receive a heartbeat from server within this timeout then server flushes that client out of memory and disconnect
event is fired automatically on client side. This timeout is set when client connects to server. So you can set these properties in your Node.js server like this:
var socket = require('socket.io').listen(80,{
'heartbeat interval': 5,
'heartbeat timeout' : 10
});
Now server will emit heartbeat every 5 seconds and if client doesn't receive a heartbeat in 10 seconds, disconnect event will be fired. But remember that heartbeat interval should be less than heartbeat timeout. Hope this will work!
Check the internet connection for each and every time of submission in JavaScript
You can use navigator.onLine
to check the either there is internet connection or not. You can also use 2 event listener to notify when network connection is connected or disconnected.
console.log(navigator.onLine);
// OR
window.addEventListener('online', () => { //console.log('Online');});
window.addEventListener('offline', () => { //console.log('Offline'); });
Detect the Internet connection is offline?
You can determine that the connection is lost by making failed XHR requests.
The standard approach is to retry the request a few times. If it doesn't go through, alert the user to check the connection, and fail gracefully.
Sidenote: To put the entire application in an "offline" state may lead to a lot of error-prone work of handling state.. wireless connections may come and go, etc. So your best bet may be to just fail gracefully, preserve the data, and alert the user.. allowing them to eventually fix the connection problem if there is one, and to continue using your app with a fair amount of forgiveness.
Sidenote: You could check a reliable site like google for connectivity, but this may not be entirely useful as just trying to make your own request, because while Google may be available, your own application may not be, and you're still going to have to handle your own connection problem. Trying to send a ping to google would be a good way to confirm that the internet connection itself is down, so if that information is useful to you, then it might be worth the trouble.
Sidenote: Sending a Ping could be achieved in the same way that you would make any kind of two-way ajax request, but sending a ping to google, in this case, would pose some challenges. First, we'd have the same cross-domain issues that are typically encountered in making Ajax communications. One option is to set up a server-side proxy, wherein we actually ping
google (or whatever site), and return the results of the ping to the app. This is a catch-22 because if the internet connection is actually the problem, we won't be able to get to the server, and if the connection problem is only on our own domain, we won't be able to tell the difference. Other cross-domain techniques could be tried, for example, embedding an iframe in your page which points to google.com, and then polling the iframe for success/failure (examine the contents, etc). Embedding an image may not really tell us anything, because we need a useful response from the communication mechanism in order to draw a good conclusion about what's going on. So again, determining the state of the internet connection as a whole may be more trouble than it's worth. You'll have to weight these options out for your specific app.
Related Topics
ASP.NET Urlencode Ampersand for Use in Query String
C# Windows Forms Application - Updating Gui from Another Thread and Class
Differencebetween Manualresetevent and Autoresetevent in .Net
Auto Create Database in Entity Framework Core
How to Pronounce "=>" as Used in Lambda Expressions in .Net
How to Calculate the Ip Range When the Ip Address and the Netmask Is Given
Embed Unity3D App Inside Wpf Application
What's the Best Way to Calculate the Size of a Directory in .Net
What Is the Use of the Arraysegment<T> Class
How to Receive Plug & Play Device Notifications Without a Windows Form
Which Is the Correct C# Infinite Loop, for (;;) or While (True)
How to Round Up Value C# to the Nearest Integer
Anonymous Types - Are There Any Distingushing Characteristics
ASP.NET Core 3.0 System.Text.JSON Camel Case Serialization