Secure sockets with Adobe AIR 2.0
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>




