Internet Connection Status

From Director Online Wiki
Jump to: navigation, search

This parent script allows a developer to correctly detect the internet connection, as well as get the public IP of the interface device (router) connected to the internet.

Be aware that the environment's #internetConnected property does not give a true indication if you are connected to the internet or not... it simply detects the connection status of your local area network (ie. if your network cable is plugged in at both ends properly). Your internet connection can be down and #internetConnected will still display #online if plugged into the router or modem.

The zipped file contains a .dir file with the parent script and a bare bones example of it in action. Open the script and read the extensive comments to get a better understanding of how to use this parent script.

Test Internet Connection v1.1 - Parent Script (30KB)

-- to get the methods and other information about the 
-- parent script type this in the message window:
tncObj = script("test.net.connection").new()
put tncObj.interface()
 
-- "
-- Parent Script: Test Internet Connection
-- Version: 1.1
-- Copyright: ©2009 Josh Chunick
-- Description: This script will test for an internet connection and return a 
-- property list with the getStreamStatus() result as the #streamStatus property 
-- value, and three other properties; #internetConnected, #publicIP, and #elapsedTime.
-- 
-- Public Methods:
-- ---------------
-- new() - creates a new instance of the parent script.
-- getTimeout() - gets the timeout in milliseconds for each getNetText() call of a url in the url list. Default is 1000ms.
-- setTimeout(integer) - sets the timeout in milliseconds for each getNextText() call of a url in the url list. 
-- Value is limited from 500 to 5000ms.
-- getPeriod() - gets the period value in milliseconds for the timeout object used to update the callback handler. 
-- Default is 1000ms.
-- setPeriod(integer) - sets the period value in milliseconds for the timeout object. Value is limited from 10 to 500 ms.
-- getBypassNumber() -  gets the number that will bypass the url list count during testing.
-- setBypassNumber(integer) - sets the number that will bypass the url list count during testing. Value is limited from  
-- 1 to the number of items in the url list.
-- getUrlList() - gets a dulicate copy of the url list.
-- setUrlList(list) - sets a list of urls as the current url list.
-- addUrl(string) - adds a url to the url list.
-- restoreUrlList() - restores the duplicate url list to the default url list.
-- setCallback(#handlerName, script) - sets the specified script and it's handler to use as the callback handler.
-- testConnection() - starts the internet connection test.
-- abortTest() - aborts the internet connection test.
-- errorCode() - returns the error string if an error value (not 0) is returned from a method.
-- version() - returns the parent script version as a string.
-- interface() - returns this string of information about the parent script and it's public methods.
 
-- Parent Script Usage:
-- --------------------
-- Add below code to a button, for instance:
-- on mouseUp me
--   objIC = script("test.net.connection").new()
--   -- other 'set' methods should go here, before testConnection()
--   objIC.testConnection()
--   objIC = 0
-- end
-- 
-- Add below code to a movie script named 'Internet Connected Status':
-- on internetConnectedStatus lst
--   put lst
-- end
-- 
-- setCallback() Defaults:
-- -----------------------
-- MX2004 and higher: me.setCallback(#internetConnectedStatus, _movie)
-- MX and lower: setCallback(#internetConnectedStatus, script("Internet Connected Status"))
-- 
-- setCallback() Examples:
-- -----------------------
-- parentobject.setCallback(#myCallbackHandler, sprite(1)) - send the message to sprite 1.
-- parentobject.setCallback(#myCallbackHandler, script("go frame")) - send the message to the current 
-- framescript, not recommended.
-- parentobject.setCallback(#myCallbackHandler, _movie) - send the message to a moviescript handler;
-- MX2004 syntax only.
-- parentobject.setCallback(#myCallbackHandler, script("main")) - send the message to a script named 'main'.
-- parentobject.setCallback(#myCallbackHandler, script(member(3), 2)) - send the message to a script attached 
-- to member 3 of castlib 2, and the handler must have a me parameter.
 
-- Callback Output Example:
-- ------------------------
-- [#streamStatus: [#url: "http://www.whatismyip.com/automation/n09230945.asp", \
-- #state: "Complete", #bytesSoFar: 14, #bytesTotal: 14, #error: "OK"], \
-- #internetConnected: #online, #publicIP: "24.100.200.230", #elapsedTime: 574]
-- "