Flash / Flex / ActionScript/Network/XMLSocket

Материал из Web эксперт
Перейти к: навигация, поиск

Listen to the connect event

 
package {
  import flash.display.Sprite;
  import flash.events.*;
  import flash.net.XMLSocket;
  public class SocketExample extends Sprite {
  
    private var socket:XMLSocket;
  
    public function SocketExample(  ) {
      socket = new XMLSocket(  );
      
      socket.addEventListener( Event.CONNECT, onConnect );
      
      socket.connect( "localhost", 2900 );
    }
    
    private function onConnect( event:Event ):void {
      trace( "The xml socket is now connected..." );  
    }
    
  }
}
Security.loadPolicyFile("http://www.wbex.ru/crossdomain.xml");



Use XML socket Connect event and Data event

 
package {
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.DataEvent;
  import flash.net.XMLSocket;
  public class SocketExample extends Sprite {
  
    private var xmlSocket:XMLSocket;
  
    public function SocketExample(  ) {
      xmlSocket = new XMLSocket(  );
      
      xmlSocket.addEventListener( Event.CONNECT, onConnect );
      
      xmlSocket.addEventListener( DataEvent.DATA, onData );
      xmlSocket.connect( "localhost", 2900 );
    }
    
    private function onConnect( event:Event ):void {
      xmlSocket.send( "<test/>" );  
    }
    private function onData( event:DataEvent ):void {
      trace( event.data );
      
      var response:XML = new XML( event.data );
      
      trace( response.test.@success );
    }
  }
}



Use XMLSocket to retrieve data

 
package{
  import flash.display.Sprite;
  import flash.net.*;
  import flash.events.*;
  public class Main extends Sprite{
            var socket:XMLSocket = new XMLSocket();
    public function Main(){

        socket.addEventListener(Event.CONNECT, connected);
        socket.addEventListener(DataEvent.DATA, dataReceived);
        socket.connect("localhost", 8989);
    }
            private function connected(event:Event):void
        {
            socket.send("hi");
        }
        private function dataReceived(dataEvent:DataEvent):void
        {
            trace(dataEvent.data);
            var xml:XML = new XML(dataEvent.data);
        }
  }
}