Jump to content

check if server and client are connected


s_avinash_s
 Share

Recommended Posts

Hi

My requirement is to check from a web page if the server on the board is accessible or not.

If it is accessible, I need to display on my web page as "connected " else "disconnected."

How can i do this.

Is there a way to use 

xhttp.open("GET", "online_check.txt", true);

and setinterval to check every 1 second.

If i get a data from server, display as connected else as disconnected.

 

Is  it possible.

Please suggest

 

Link to comment
Share on other sites

You can send an XMLHttpRequest periodically and check that the HTTP status is 0.

Building up on the line of code you provided earlier.

// ... More code
xhttp.open("GET", "online_check.txt", true);
xhhtp.onreadystatechange = function() {
  if(xhttp.readyState == 4) {
    if(xhttp.status == 0) {
      // Show disconnected message
    } else {
      // Show connected message
    }
  }
}
// ... More code

To save on server resources, you probably should not make requests more frequently than once every 5 seconds.

Link to comment
Share on other sites

Hi Dsonesuk and Ingolme.

1. My board has a server type which accepts GET and POST requests and Web page acts as client.

I did not get what do you mean by card server.

2.I tried like a below code.when i press the button nothing appears

    
        <p id="ping1">
        <button type="button" onclick="ping()">Change Content</button>
        </p>
    <script>
    function ping() {

      var xhttp = new XMLHttpRequest();
     xhhtp.onreadystatechange = function() {
  if(xhttp.readyState == 4) {
    if(xhttp.status == 0) {
      
      document.getElementById("ping1").innerHTML = "Disconnected";
    } else {
      
      document.getElementById("ping1").innerHTML = "Connected";
    }
  }
    xhttp.open("GET", "ping.txt", true);
    xhttp.send();
    }
    </script>

 

Link to comment
Share on other sites

Hi

I have checked the brackets and changed long back  but still nothing is happening.

        <p id="ping1">
        <button type="button" onclick="ping()">Change Content</button>
        </p>
    <script>
    function ping() 
    {
      var xhttp = new XMLHttpRequest();
      xhhtp.onreadystatechange = function() 
        {
            if(xhttp.readyState == 4) 
            {
                if(xhttp.status == 0) 
                {      
                    document.getElementById("ping1").innerHTML = "Disconnected";
                }
                else 
                {     
                    document.getElementById("ping1").innerHTML = "Connected";
                }
            }
        };
    xhttp.open("GET", "ping.txt", true);
    xhttp.send();
    }
        
    </script>

 

Link to comment
Share on other sites

  • 3 weeks later...

Hi 

I have got a sample code for LED  change.

https://www.w3schools.com/code/tryit.asp?filename=FW1SQNHXYORW

How to get led change in below conditions using div as shown above.

 if(xhttp.status == 0) 
                {      
                    document.getElementById("ping1").innerHTML = "Disconnected";
                    document.getElementById("ping1").style.color = "red";                  
                }
                else 
                {       
                    document.getElementById("ping1").innerHTML = "Connected";                  
                    document.getElementById("ping1").style.color = "green";                                       
                }

In above if status is 0 , i need to glow yellow LED else Green .How to do it.

Please help

Link to comment
Share on other sites

Hi

I created <p> with id and now i can see it once at the starting, may be due p with id creation.

disconnected is always visible.

Now after disconnected is shown , i want led to be glown continously

<p  id="led-yellow"></p>
<p  id="ping1"></p>
    <script>     
    function ping() 
    {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() 
        {
            if(xhttp.readyState == 4) 
            {
                if(xhttp.status == 0) 
                {      
                    document.getElementById("ping1").innerHTML = "Disconnected"; 
                    document.getElementById("led-yellow").style.visibility = "hidden";
                }
                else 
                {        
                    document.getElementById("ping1").innerHTML = "Connected";  
                    document.getElementById("led-green").style.visibility = "hidden";
                }
            }
        };
    xhttp.open("GET", "ping.txt", true);
    xhttp.send();
    }
     setInterval("ping()", 1000);       
    </script>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...