0

My first post of these forums :)

This is to work in a mobile browser (android in this case). I need to check if a computer is on before I know which function to use.

Is there a way I can ping it so as to determine it's network presence and run an according function? For now, I just have messages for testing. (navigator.notification.alert is the Cordova method for displaying messages on a mobile device)

I've sort of made up this code, is there any way to get this to work, I've done a bit of Googling to no avail.

Please note that this is to work on a local network.

function isOn() {
    $.ajax({
        url : 'http://192.168.x.xxx' ,              //x.xxx being the client ip
        success : function () {
            navigator.notification.alert(
        'The computer is on.',              //Message body
        alertDismissed,                 //Callback
        'The Computer is On',               //Title
        'Yes'                       //Button Label
        );
        },
        error : function () {
            navigator.notification.alert(
            'The computer is off.',         //Message body
            alertDismissed,             //Callback
            'The Computer is Off',          //Title
            'Yes'                   //Button Label
            );
        }
    });
};


<a onclick="isOn();">Test</a>
Mike
  • 117
  • 1
  • 1
  • 8
  • What about that code doesn't work? Does it give any errors in the console? – sachleen Aug 13 '12 at 15:01
  • possible duplicate of [Javascript: Check if server is online?](http://stackoverflow.com/questions/5224197/javascript-check-if-server-is-online) – sachleen Aug 13 '12 at 15:01
  • Here is another SO question related to pinging from JS: http://stackoverflow.com/questions/4954741/how-to-ping-ip-addresses-using-javascript – jwatts1980 Aug 13 '12 at 15:02

1 Answers1

0

The url can't be just an IP. You must request a document. If your client doesn't has a working web server you will get an error.

So, if you want to ping a computer, you must call a server-side script:

$.post('http://myserver.com/ping.php', {'ip': '111.222.123.123'}, function(data){
  // your code
});

on the server-side you can use something like this:
http://www.codediesel.com/php/ping-a-server-using-php/ to ping clients and then return the results JSON encoded.

Peter
  • 5,138
  • 5
  • 29
  • 38