0 Replies Latest reply on Sep 5, 2007 4:15 AM by Sprit.H

    AS3 socket Issue????????Help Pls!!

    Sprit.H
      I am working with a MMORPG game used AS3 Socket,but I find that AS3 Socket can not continuou receive data ,but It work well when i used AS3 XMLSocket,What is the problem??Pls give me a hand,thx!!(the code below)


      package {
      import flash.display.Sprite;

      public class SocketExample extends Sprite {

      public function SocketExample() {
      var socket:CustomSocket = new CustomSocket("10.4.0.20", 5555);
      }
      }
      }

      import flash.errors.*;
      import flash.events.*;
      import flash.net.Socket;

      class CustomSocket extends Socket {
      private var response:String;

      public function CustomSocket(host:String = null, port:uint = 0) {
      super(host, port);
      configureListeners();
      }

      private function configureListeners():void {
      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 sendRequest():void {
      trace("sendRequest");
      response = "";
      writeln("login a a");
      flush();
      }

      private function readResponse():void {
      var str:String = readUTFBytes(bytesAvailable);
      response += str;
      }


      private function connectHandler(event:Event):void {
      trace("connectHandler: " + event);
      sendRequest();
      }



      private function socketDataHandler(event:ProgressEvent):void {
      trace("get")
      trace("socketDataHandler: " + event);
      trace(this.readUTFBytes(this.bytesAvailable));
      readResponse();
      }
      }

      output----------------------------------------------------------------------------------
      connectHandler: [Event type="connect" bubbles=false cancelable=false eventPhase=2]
      sendRequest
      get
      socketDataHandler: [ProgressEvent type="socketData" bubbles=false cancelable=false eventPhase=2 bytesLoaded=220 bytesTotal=0]
      5111 812u#70000005050010001508015003030obj/map/testmap({p#8})p#8 393p#80u#70000505000010060


      ~~~~~~~~~~XMLSocket

      package {
      import flash.display.Sprite;
      import flash.events.*;
      import flash.net.XMLSocket;

      public class XMLSocketExample extends Sprite {
      private var hostName:String = "10.4.0.20";
      private var port:uint = 5555;
      private var socket:XMLSocket;

      public function XMLSocketExample() {
      socket = new XMLSocket();
      configureListeners(socket);
      socket.connect(hostName, port);
      }

      public function send():void {
      socket.send("login a a"+"\r");
      }


      private function configureListeners(dispatcher:IEventDispatcher):void {
      dispatcher.addEventListener(Event.CLOSE, closeHandler);
      dispatcher.addEventListener(Event.CONNECT, connectHandler);
      dispatcher.addEventListener(DataEvent.DATA, dataHandler);
      dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
      dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
      dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
      }


      private function connectHandler(event:Event):void {
      trace("链接成功");
      send();
      }

      private function dataHandler(eventataEvent):void {
      trace("get")
      trace("dataHandler: " + event);
      }

      private function ioErrorHandler(event:IOErrorEvent):void {
      trace("ioErrorHandler: " + event);
      }

      private function progressHandler(event:ProgressEvent):void {
      trace("链接成功");
      trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
      }

      }
      }

      output---------------------------------------------------
      链接成功
      get
      dataHandler: [DataEvent type="data" bubbles=false cancelable=false eventPhase=2 data=" 5111 842u#110000005050010001508015003030obj/map/testmap({p#12})p#12 413p#120u#110000505000010060"]
      get
      dataHandler: [DataEvent type="data" bubbles=false cancelable=false eventPhase=2 data=" 2211obj/map/testmap00"]
      get
      dataHandler: [DataEvent type="data" bubbles=false cancelable=false eventPhase=2 data=" 175u#10003030 185u#110003030"]