Socket Progress Events

The flash.net.Socket class has been available to us since the initial release of Adobe AIR. When using sockets in ActionScript, developers have always had access to an event to monitor the progress of input data coming through the socket by using flash.events.ProgressEvent. Up until now, however, monitoring the output data being sent across a socket connection has been nearly impossible.

With AIR 3, we now have access to the flash.events.OutputProgressEvent class, with which we can easily monitor both the pending and total bytes being sent out over a socket connection. This can be used in order to display something like a progress indicator to the user, sequence certain events within an application, or simply verify that the data has been fully processed over the socket.

In the following example, we establish a socket connection and monitor both the flash.events.ProgressEvent and flash.events.OutputProgressEvent events when connected to adobe.com using a basic socket connection.

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.OutputProgressEvent;
    import flash.events.ProgressEvent;
    import flash.net.Socket;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.utils.ByteArray;

    [SWF(width="600", height="500", backgroundColor="#CCCCCC")]

    public class SocketProgress extends Sprite {

        private var traceField:TextField;
        private var socket:Socket;

        public function SocketProgress() {
            generateDisplayObjects();
            performOperations();
        }

        protected function generateDisplayObjects():void {
            var defaultFormat:TextFormat = new TextFormat();
            defaultFormat.font = "Arial";
            defaultFormat.size = 22;
            defaultFormat.color = 0xFFFFFF;

            traceField = new TextField();
            traceField.backgroundColor = 0x000000;
            traceField.alpha = 0.7;
            traceField.width = stage.stageWidth;
            traceField.height = stage.stageHeight;
            traceField.wordWrap = true;
            traceField.multiline = true;
            traceField.background = true;
            traceField.defaultTextFormat = defaultFormat;
            addChild(traceField);
        }

        protected function performOperations():void {
            socket = new Socket();
            socket.addEventListener(Event.CONNECT, socketConnected);
            socket.addEventListener(IOErrorEvent.IO_ERROR, socketError);
            socket.addEventListener(ProgressEvent.SOCKET_DATA, socketProgress);

            socket.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, socketOutputProgress);

            socket.connect("adobe.com", 80);
            traceField.text = "Attempting Connection...

";
        }

        protected function socketProgress(e:ProgressEvent):void {
            var byteArray:ByteArray = new ByteArray();
            traceField.appendText("SOCKET DATA RETURNED:
");
            socket.readBytes(byteArray, 0, socket.bytesAvailable);
            traceField.appendText(byteArray.toString());
        }

        protected function socketOutputProgress(e:OutputProgressEvent):void {
            traceField.appendText("OUTPUT PROGRESS => bytesPending: " + e.bytesPending + " / bytesTotal: " + e.bytesTotal + "

");
        }

        protected function socketConnected(e:Event):void {
            traceField.appendText("Socket Connected!

");
            socket.writeUTFBytes("GET/HTTP/1.1
Host: adobe.com");
        }


        protected function socketError(e:IOErrorEvent):void {
            traceField.appendText("IO Error: " + e.text + "

");
        }

    }
}

The result of the above code is shown in Figure 7-3. As we can see, we now have the ability to monitor the bytes being returned over our established socket as well as access the total bytes to expect from this transaction.

Socket output progress

Figure 7-3. Socket output progress

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset