locked
VS 2013 LightSwitch - Ping IP from HTML application RRS feed

  • Question

  • Hello everyone, I am working an HTML application that list some IP's in a table. I would like to be able to select that IP and click on a button which pings the selected IP. On success display message "Active" or "Inactive" on no response. Is this possible?

    Thanks


    Larry

    Wednesday, October 21, 2015 7:12 PM

Answers

  • Hey Larry,

    You will not be able to do this from the browser so it will have to take place on the server. In order to perform a ping, you will use what's called a "Commanding Pattern".

    On your IPAddresses table add a field called "Action" a string data type and another field called "PingResponse" as data type Boolean.

    On the server, add a IPAddresses_Updating() method and write some code like this.

    partial void IPAddresses_Updating(IPAddress entity)
    {
        if(entity.Action == "Ping")
        {
             try
             {
                 var ping = new Ping();
                 var reply = ping.Send(entity.Address);
                 entity.PingResponse = reply.Status == IPStatus.Success;
             }
    
             catch
             {
                 entity.PingResponse = false;
             }
    
             finally
             {
                 entity.Action = string.Empty;
             }
        }
    }
    

    Please keep in mind this is not tested and is rather rudimentary.

    What you will do to ping, is update the IPAddresses table and set the Action to "Ping". After you update, you can look at the PingResponse field on the client  to get the result. Keep in mind that there is a lot more data in the PingReply that can be added to your IPAddress table.

    Thursday, October 22, 2015 2:52 AM

All replies

  • Hey Larry,

    You will not be able to do this from the browser so it will have to take place on the server. In order to perform a ping, you will use what's called a "Commanding Pattern".

    On your IPAddresses table add a field called "Action" a string data type and another field called "PingResponse" as data type Boolean.

    On the server, add a IPAddresses_Updating() method and write some code like this.

    partial void IPAddresses_Updating(IPAddress entity)
    {
        if(entity.Action == "Ping")
        {
             try
             {
                 var ping = new Ping();
                 var reply = ping.Send(entity.Address);
                 entity.PingResponse = reply.Status == IPStatus.Success;
             }
    
             catch
             {
                 entity.PingResponse = false;
             }
    
             finally
             {
                 entity.Action = string.Empty;
             }
        }
    }
    

    Please keep in mind this is not tested and is rather rudimentary.

    What you will do to ping, is update the IPAddresses table and set the Action to "Ping". After you update, you can look at the PingResponse field on the client  to get the result. Keep in mind that there is a lot more data in the PingReply that can be added to your IPAddress table.

    Thursday, October 22, 2015 2:52 AM
  • As Ian noted, server-side is how to do a true ping and there are many benefits like additional data being saved in your entities.

    However, if you really need to know if an IP is reachable from the client you could use something like this javascript:

    function ping(host, port, pong) {
    //example call
    //ping("192.168.1.1", "80", function(m){ console.log("It took "+m+" miliseconds."); })
    
      var started = new Date().getTime();
    
      var http = new XMLHttpRequest();
    
      http.open("GET", "http://" + host + ":" + port, /*async*/true);
      http.onreadystatechange = function() {
        if (http.readyState == 4) {
          var ended = new Date().getTime();
    
          var milliseconds = ended - started;
    
          if (pong != null) {
            pong(milliseconds);
          }
        }
      };
      try {
        http.send(null);
      } catch(exception) {
        // this is expected
      }
    
    }

    I have no idea how reliable this is.

    More info:

    http://stackoverflow.com/questions/4282151/is-it-possible-to-ping-a-server-from-javascript

    HTH,

    Josh

    Friday, October 23, 2015 10:56 AM