topical media & game development

talk show tell print

actionscript-socket-CustomSocket.ax

actionscript-socket-CustomSocket.ax [swf] flex


  package {
  import flash.errors.*;
  import flash.events.*;
  import flash.net.Socket;
  import mx.controls.*;
  
  public class @ax-actionscript-socket-CustomSocket extends Socket {
      private var response:String;
      private var status:TextArea
      
      private function trace(x:String):void {
      status.text += x + "\n";
      }
  
      public function @ax-actionscript-socket-CustomSocket(console:Object, host:String = null, port:uint = 0) {
          super(host, port);
          status = TextArea(console);
          trace("initialized"); 
          configureListeners();
      }
  
      private function configureListeners():void {
          
          trace("configure"); 
          addEventListener(Event.CLOSE, closeHandler);
          addEventListener(Event.CONNECT, connectHandler);
          addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
          addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
          addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
      }
  
      private function writeln(str:String):void {
          str += "\n";
          try {
              writeUTFBytes(str);
          }
          catch(e:IOError) {
              trace(String(e));
          }
      }
  
      public function sendRequest():void {
          trace("sendRequest");
          response = "";
          writeln("GET /");
          //writeln("there is a lot of research to do");
          flush();
      }
  
      private function readResponse():void {
          var str:String = readUTFBytes(bytesAvailable);
          response += str;
          trace(response.toString());
      }
  
      private function closeHandler(event:Event):void {
          trace("closeHandler: " + event);
          trace(response.toString());
      }
  
      private function connectHandler(event:Event):void {
          trace("connectHandler: " + event);
          sendRequest();
      }
  
      private function ioErrorHandler(event:IOErrorEvent):void {
          trace("ioErrorHandler: " + event);
      }
  
      private function securityErrorHandler(event:SecurityErrorEvent):void {
          trace("securityErrorHandler: " + event);
      }
  
      private function socketDataHandler(event:ProgressEvent):void {
          trace("socketDataHandler: " + event);
          readResponse();
      }
  }
  
  }
  


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.