Difference between revisions of "Function ping()"

From wiki.netio-products.com
Jump to navigation Jump to search
(Založena nová stránka s textem „{{DISPLAYTITLE:function ping()}} Test connection to specified IP or URL using ICMP packet <i>nil</i> ping{<u>config</u>} === Parameters === <i>table</i…“)
 
Line 2: Line 2:
 
Test connection to specified IP or URL using ICMP packet
 
Test connection to specified IP or URL using ICMP packet
  
  <i>nil</i> ping{<u>config</u>}
+
  <i>nil</i> ping{<code>config</code>}
  
 
=== Parameters ===
 
=== Parameters ===
  <i>table</i> <u>config</u>
+
  <i>table</i> <tt>config</tt>
* <i>string</i> <u>address</u> IP or URL (without http://) to test
+
* <i>string</i> <code>address</code> IP or URL (without http://) to test
* optional <i>number</i> <u>timeout</u> maximal wait for response (in seconds)
+
* optional <i>number</i> <code>timeout</code> maximal wait for response (in seconds)
* <i>function{response}</i> <u>callback</u> this function is called when response arrives or timeout is reached
+
* <i>function{response}</i> <code>callback</code> this function is called when response arrives or timeout is reached
  
 
=== Callback Parameters ===
 
=== Callback Parameters ===
  <i>table</i> <u>response</u>
+
  <i>table</i> <tt>response</tt>
* <i>boolean</i> <u>success</u> false = timeout reached, true = response confirmed before timeout
+
* <i>boolean</i> <code>success</code> false = timeout reached, true = response confirmed before timeout
* <i>number</i> <u>duration</u> response delay (in milliseconds); if not success, duration is <tt>nil</tt>
+
* <i>number</i> <code>duration</code> response delay (in milliseconds); if not success, duration is <tt>nil</tt>
* <i>string</i> <u>errorInfo</u> error info in case of failure. If success, it contains "No error, return OK"
+
* <i>string</i> <code>errorInfo</code> error info in case of failure. If success, it contains "No error, return OK"
  
 
=== Return value ===
 
=== Return value ===

Revision as of 20:41, 18 April 2017

Test connection to specified IP or URL using ICMP packet

nil ping{config}

Parameters

table config
  • string address IP or URL (without http://) to test
  • optional number timeout maximal wait for response (in seconds)
  • function{response} callback this function is called when response arrives or timeout is reached

Callback Parameters

table response
  • boolean success false = timeout reached, true = response confirmed before timeout
  • number duration response delay (in milliseconds); if not success, duration is nil
  • string errorInfo error info in case of failure. If success, it contains "No error, return OK"

Return value

nil

Usage

-- callback function
local function logPingResult(o)
  if o.success then
    logf("Response lag: %dms", o.duration)
  else
    logf("Ping failed, reason: %s", o.errorInfo)
  end
end

-- test ping www.google.com
ping{address="www.google.com", timeout=10, callback=logPingResult}

-- test ping ip address
ping{address="77.75.79.39", callback=logPingResult}

-- test ping non-existent address
ping{address="Pandora", timeout=2, callback=logPingResult}