Subash
Subash

Reputation: 177

Check LAN connection by javascript

I have deployed a web application in a machine, which is connected to all the other client machine in the LAN. While client is using the web app through browser I need to check whether the LAN connection exist for each request by javascript.

I have googled a lot about this, but all the content are regarding the internet connection and not regarding the LAN connection. Thanks in advance.

Upvotes: 1

Views: 894

Answers (1)

Muhammad Usman
Muhammad Usman

Reputation: 1362

Try this one : Works for me :)

function hostReachable() {

  // Handle IE and more capable browsers
  var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );
  var status;

  // Open new request as a HEAD to the root hostname with a random param to bust the cache
  xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false );

  // Issue request and handle response
  try {
    xhr.send();
    return ( xhr.status >= 200 && xhr.status < 300 || xhr.status === 304 );
  } catch (error) {
    return false;
  }

}

Reference

Upvotes: 1

Related Questions