Jump to content

check if server and client are connected


s_avinash_s

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

If you don't refresh the page, it won't know if connection is lost! it would be like being on a web page and disconnecting a cable to your pc from router or router to external fibre or telephone/data cable, there won't be no indication that access has been broken until you refresh the page itself.

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

Are we working to a scenario that

{1) The board has a server type functionality that reads post requests

(2) The card has a local server.

(3) The "web page" works from card local server.

(4) You want to determine if connection between card server and board server exists or not by any means.

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

Count the curly braces '{}' they are uneven, meaning one is missing.

Your mission if you accept it! Is to find which curly brace is missing and add it to the script in the correct position, as usual if you or any of your IM force are captured we will deny any knowledge of your existence.

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

Then you need to apply invisibility: hidden; property to to the css styling for both LED's. You then need to under the if status 4 condition, make sure both are hidden again, to allow for connection changes, then if disconnected or connected make then visible NOT! hidden.

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
×
×
  • Create New...