<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.24343-</id>
  <updated>2009-11-16T15:39:40Z</updated>
  <title>Comments for Adobe AIR in Action: Monitoring Sockets (http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.24343</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=24343" title="Adobe AIR in Action: Monitoring Sockets" />
    <published>2008-07-07T19:30:00Z</published>
    <updated>2008-09-30T18:51:14Z</updated>
    <title>Adobe AIR in Action: Monitoring Sockets</title>
    <summary>The internet is a great place, but it has two drawbacks.  You can&#8217;t access it everywhere, and sometimes sites and services go down.  Modern web-enabled applications need to expect lapses in network connectivity.  So it is not a surprise that the ability to gracefully handle sometimes-connectedness is one of the most heralded features of AIR.

I started exploring AIR&#8217;s network detection classes while co-writing Adobe AIR in Action, and was reminded that a lot of neat communication can happen over sockets.  </summary>
    <author>
      <name>Kathryn Rotondo</name>
      
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<div class="ap_r"><a href="http://www.insideria.com/upload/2008/07/lott.jpg" class="highslide" onclick="return hs.expand(this)"><a href="http://manning.com/lott" target="_blank"><img src="http://www.insideria.com/upload/2008/07/lott.jpg" alt="lott.jpg" title="Click to enlarge" width="148"/></a></div>The internet is a great place, but it has two drawbacks.  You can&#8217;t access it everywhere, and sometimes sites and services go down.  Modern web-enabled applications need to expect lapses in network connectivity.  So it is not a surprise that the ability to gracefully handle sometimes-connectedness is one of the most heralded features of AIR.
<br/><br/>
<p>I started exploring AIR&#8217;s network detection classes while co-writing <a href="http://manning.com/lott" target="_blank">Adobe AIR in Action</a>, and was reminded that a lot of neat communication can happen over sockets.  You can connect to many servers on ports other than the standard HTTP port to access a variety of services.  For example, you can connect to a National Institutes of Standards and Times (NIST) server on port 13 to get the current time and date.</p>

<p>Writing an application that checks whether the NIST server is available, before connecting the socket to get the time, is easily accomplished in AIR.  First, you need to import the SocketMonitor class, which resides in an archive file called servicemonitor.swc.  Flex Builder includes this library automatically. Flex SDK users can find it in the frameworks/libs/air directory, while Flash users will see it in the AIK/frameworks/libs/air directory of the Flash CS3 installation directory.</p>

<p>Next you can get the monitor up and running in three easy steps:</p>

<ol>
<li>Create a SocketMonitor instance, passing the server and port to the constructor.</li>
<li>Add a listener for status events on the monitor. In the corresponding handler function, access the monitor&#8217;s Boolean <code>available</code> property.</li>
<li>Start the monitor.</li>
</ol>

<p>The monitor&#8217;s <code>available</code> property is set based on the results of polling the connection.  By default the monitor only polls when it is first started and when it is informed of network status changes.  You can optionally set the monitor&#8217;s <code>pollInterval</code> to check the status more often.</p>

<p>After confirming that you can connect to the server, the only remaining task is to retrieve the time.  This is just a matter of connecting the socket, listening to its data events, and parsing the data that comes across.  The below example does just that.  As long as it is online, it uses a timer to retrieve the updated time every ten seconds.</p>

<p>Here is the full application:</p>
 <div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre> 
&lt;?xml <span class="category2">version</span>="<span class="quote">1.0</span>" encoding="<span class="quote">utf-8</span>"?&gt;
&lt;mx:WindowedApplication xmlns:mx="<span class="quote">http://www.adobe.com/2006/mxml</span>" layout="<span class="quote">absolute</span>" creationComplete="<span class="quote">creationCompleteHandler();</span>"&gt;

	&lt;mx:Script&gt;
		&lt;![CDATA[
		<span class="category1">import</span> air.net.SocketMonitor;

		<span class="category1">private</span> <span class="category1">var</span> _monitor:SocketMonitor;
		<span class="category1">private</span> <span class="category1">var</span> _socket:Socket;
		<span class="category1">private</span> <span class="category1">var</span> _timer:Timer;
		<span class="category1">private</span> const SERVER:<span class="category2">String</span> = "<span class="quote">time-A.timefreq.bldrdoc.gov</span>";
		<span class="category1">private</span> const PORT:<span class="category1">int</span> = 13;
			
		<span class="category1">private</span> <span class="category1">function</span> creationCompleteHandler():<span class="category1">void</span> {
 		    _monitor = <span class="category1">new</span> SocketMonitor(SERVER, PORT);
 		    _monitor.addEventListener(StatusEvent.STATUS, statusHandler);
 		    _monitor.<span class="category2">start</span>();
 
 		    _socket = <span class="category1">new</span> Socket();
 		    _socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
 
 		    _timer = <span class="category1">new</span> Timer(10000);
 		    _timer.addEventListener(TimerEvent.TIMER, timerHandler);
 		}
			
		<span class="category1">private</span> <span class="category1">function</span> statusHandler(event:StatusEvent):<span class="category1">void</span> {
 		    <span class="category1">if</span>(!_monitor.available) {
  		        timeText.<span class="category2">text</span> = "<span class="quote">Server unavailable. Will reconnect when the server is next available.</span>";
  		        _timer.<span class="category2">stop</span>();
  		    }
 		    <span class="category1">else</span> {
  		        timerHandler();
  		        _timer.<span class="category2">start</span>();
  		    }
 		}
			
		<span class="category1">private</span> <span class="category1">function</span> socketDataHandler(event:ProgressEvent):<span class="category1">void</span> {
 		    <span class="category1">var</span> fullTime:<span class="category2">String</span> = _socket.readUTFBytes(_socket.bytesAvailable);
 		    <span class="category1">var</span> <span class="category2">time</span>:<span class="category2">String</span> = fullTime.<span class="category2">split</span>("<span class="quote"> </span>")[2];
 		    timeText.<span class="category2">text</span> = "<span class="quote">The current time (UTC) is: </span>" + <span class="category2">time</span>;
 		    _socket.<span class="category2">close</span>();
 		}
			
		<span class="category1">private</span> <span class="category1">function</span> timerHandler(event:TimerEvent = <span class="category1">null</span>):<span class="category1">void</span> {
 		    <span class="category1">if</span>(_monitor.available &amp;&amp; !_socket.connected) {
  		        _socket.<span class="category2">connect</span>(SERVER, PORT); 
  		    }
 		}
		]]&gt;
	&lt;/mx:Script&gt;
	
	&lt;mx:Text id="<span class="quote">timeText</span>" <span class="category2">width</span>="<span class="quote">100%</span>" <span class="category2">height</span>="<span class="quote">100%</span>" /&gt;

&lt;/mx:WindowedApplication&gt;</pre>
</code>

</div></div> 

<p>While the program can&#8217;t retrieve the time if the NIST server is down, it can seamlessly handle any lapses in connectivity.  Try disabling and enabling your internet connection while the application is running, and you will see that it responds accordingly.  This basic example demonstrates the simplicity and usefulness of detecting network connections in an AIR application.</p>

<p>To learn more about detecting network connectivity and other AIR functionality, you can order your copy of our book at <a href="http://manning.com/lott" target="_blank">Manning Publications</a> (shipping in late July).  The ebook is available July 13.]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24343-comment:2018600</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24343" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html#comment-2018600" />
    <title>Comment from AJ on 2008-07-09</title>
    <author>
        <name>AJ</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Author description linked to a wrong page.</p>]]>
    </content>
    <published>2008-07-09T10:24:00Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24343-comment:2018617</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24343" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html#comment-2018617" />
    <title>Comment from Kathryn on 2008-07-09</title>
    <author>
        <name>Kathryn</name>
        <uri>http://kathrynrotondo.com/weblog</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://kathrynrotondo.com/weblog">
        <![CDATA[<p>Hi AJ, I noticed that too.  The good folks at O'Reilly are fixing it; in the meantime you can find my bio at <a href="http://www.oreillynet.com/pub/au/3487.">http://www.oreillynet.com/pub/au/3487.</a></p>]]>
    </content>
    <published>2008-07-10T03:27:57Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24343-comment:2018937</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24343" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html#comment-2018937" />
    <title>Comment from Sean on 2008-07-17</title>
    <author>
        <name>Sean</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Is there a way in AIR to set AIR Application's HTTP Proxy Server setting for something like Download Manager Use Case? Appreciate your response.</p>]]>
    </content>
    <published>2008-07-17T07:12:05Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24343-comment:2054918</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24343" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html#comment-2054918" />
    <title>Comment from KIELSOFT on 2009-03-11</title>
    <author>
        <name>KIELSOFT</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>Please can you respond to Sean post, asking about HTTP proxy server setting for Air.</p>

<p>And can you please display how the above code can be used in Flash for Air.</p>]]>
    </content>
    <published>2009-03-11T12:53:39Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.24343-comment:2055229</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.24343" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/07/adobe-air-in-action-monitoring.html#comment-2055229" />
    <title>Comment from EZEKIEL on 2009-03-15</title>
    <author>
        <name>EZEKIEL</name>
        <uri></uri>
    </author>
    <content type="html" xml:lang="en" xml:base="">
        <![CDATA[<p>CAN YOU PLS RESPOND TO SEAN POST ABOUT HTTP PROXY SERVER WITH AIR</p>]]>
    </content>
    <published>2009-03-16T00:31:52Z</published>
  </entry>

</feed
