The Space For App Developers

Beyond Plain Old HTML Objects

Secure sockets with Adobe AIR 2.0

with 2 comments

During my journey of discovering the goodness of AIR 2.0 I wanted to try the new secure sockets functionality. AIR 2 comes with a new class, SecureSocket, which actually extends the standard Socket class. The easiest way for me to test it was to try to connect to GMail’s IMAP server. I was actually inspired by very cool example built by Christian Cantrell called MenuMail available here to download from Adobe Labs. I think the code below is self explaining; when you click Connect button you should see following trace output in the console:
onConnect [Event type="connect" bubbles=false cancelable=false eventPhase=2]
onSocketData [ProgressEvent type="socketData" bubbles=false cancelable=false eventPhase=2 bytesLoaded=66 bytesTotal=0]
* OK Gimap ready for requests from x.x.x.x n12if410166gve.29

Below is the source code:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:halo="library://ns.adobe.com/flex/halo">
 
	<fx:Script>
		<![CDATA[
			import flash.events.Event;
			import flash.events.IOErrorEvent;
			import flash.events.MouseEvent;
			import flash.events.ProgressEvent;
			import flash.events.SecurityErrorEvent;
			import flash.net.SecureSocket;
			import flash.net.Socket;
 
			protected var secureSocket:SecureSocket;
 
			protected function btnConnect_clickHandler(event:MouseEvent):void
			{
				secureSocket = new SecureSocket();
				secureSocket.addEventListener(Event.CONNECT, onConnect);
				secureSocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
				secureSocket.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
				secureSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
				secureSocket.connect(txtServerAdress.text, parseInt(txtServerPort.text));
			}
 
			/**
			 * Handles Event.CONNECT event when successful connection is established
			 */
			protected function onConnect(event:Event):void 
			{
				trace("onConnect", event);
			}
 
			/**
			 * Handles ProgressEvent.SOCKET_DATA when data is received through the SecureSocket
			 */
			protected function onSocketData(event:ProgressEvent):void 
			{
				trace("onSocketData", event);
 
				var socket:Socket = event.target as Socket;
				trace(socket.readUTFBytes(event.bytesLoaded));
			}
 
			/**
			 * Handles IOErrorEvent.IO_ERROR when IO error happens during the connection
			 */
			protected function onIoError(event:IOErrorEvent):void 
			{
				trace("onIoError", event);
			}
 
			/**
			 * Handles SecurityErrorEvent.SECURITY_ERROR event on any security errors
			 */
			protected function onSecurityError(event:SecurityErrorEvent):void 
			{
				trace("onSecurityError", event);
			}
 
		]]>
	</fx:Script>
 
	<halo:Form horizontalCenter="0" verticalCenter="0">
		<halo:FormHeading label="Connection info"/>
		<halo:FormItem label="Server adress:">
			<s:TextInput id="txtServerAdress" text="imap.gmail.com" />
		</halo:FormItem>
		<halo:FormItem label="Server port:">
			<s:TextInput id="txtServerPort" text="993"/>
		</halo:FormItem>
		<halo:FormItem>
			<s:Button id="btnConnect" label="Connect" click="btnConnect_clickHandler(event)" />
		</halo:FormItem>
	</halo:Form>
 
</s:WindowedApplication>

Written by Piotr Walczyszyn

January 29th, 2010 at 4:06 pm

Posted in Examples

Tagged with

2 Responses to 'Secure sockets with Adobe AIR 2.0'

Subscribe to comments with RSS or TrackBack to 'Secure sockets with Adobe AIR 2.0'.

  1. Finally, an issue that I’m passionate about. I have seemed for info of this caliber for the last a number of hours. Your web site is enormously appreciated.

    lg 3d hdtv

    5 May 11 at 8:17 pm

  2. I would like to thank you for the efforts you’ve put in writing this website. I’m hoping the same high-grade site post from you in the upcoming as well. In fact your creative writing abilities has encouraged me to get my own site now. Really the blogging is spreading its wings quickly. Your write up is a great example of it.

Leave a Reply