flash socket 聊天(一)

今天跟着网上做了一个flash socket 聊天,主要了解下flash如何与java的socket通信。

一段代码CustomSocket.as

  1. package     
  2. {   
  3.     import flash.events.*;   
  4.     import flash.net.Socket;   
  5.     import flash.system.*;   
  6.     import flash.utils.ByteArray;   
  7.     import flash.utils.setTimeout;   
  8.        
  9.     import fl.controls.TextArea;   
  10.     import fl.core.UIComponent;   
  11.     /**
  12.      * ...
  13.      * @author DefaultUser (Tools -> Custom Arguments...)
  14.      */  
  15.     public class CustomSocket   
  16.      {   
  17.         private const CR:int = 13; // Carriage Return (CR)   
  18.         private const WILL:int = 0xFB; // 251 - WILL (option code)   
  19.         private const WONT:int = 0xFC; // 252 - WON'T (option code)   
  20.         private const DO:int    = 0xFD; // 253 - DO (option code)   
  21.         private const DONT:int = 0xFE; // 254 - DON'T (option code)   
  22.         private const IAC:int   = 0xFF; // 255 - Interpret as Command (IAC)   
  23.            
  24.            
  25.         private var serverURL:String;   
  26.         private var portNumber:int;   
  27.         private var socket:Socket;   
  28.         private var ta:TextArea;   
  29.         private var state:int = 0;   
  30.          System.useCodePage = false;   
  31.         public function CustomSocket(server:String, port:int, output:TextArea)   
  32.          {   
  33.              serverURL = server;   
  34.              portNumber = port;   
  35.              ta = output;               
  36.              socket = new Socket();   
  37.              socket.addEventListener(Event.CONNECT, connectHandler);            
  38.              socket.addEventListener(Event.CLOSE, closeHandler);   
  39.              socket.addEventListener(ErrorEvent.ERROR, errorHandler);   
  40.              socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);   
  41.              socket.addEventListener(ProgressEvent.SOCKET_DATA, dataHandler);   
  42.                
  43.              Security.loadPolicyFile("http://" + serverURL + ":" + portNumber + "/crossdomain.xml");   
  44.                
  45.             try  
  46.              {   
  47.                  msg("Trying to connect to" + serverURL + ":" + portNumber + "/n");   
  48.                  socket.connect(serverURL,portNumber);   
  49.              }   
  50.             catch (error:Error)   
  51.              {   
  52.                  msg(error.message + "/n");   
  53.                  socket.close();   
  54.              }   
  55.          }   
  56.         public function ioErrorHandler(event:IOErrorEvent):void  
  57.          {   
  58.              msg("Unable to connect: socket error./n");   
  59.          }   
  60.          public function writeBytesToSocket(ba:ByteArray):void {   
  61.               trace(ba);   
  62.              socket.writeBytes(ba);   
  63.              socket.flush();   
  64.          }   
  65.          private function connectHandler(event:Event):void {               
  66.             if (socket.connected) {   
  67.                  msg("connected.../n");                 
  68.              } else {   
  69.                  msg("unable to connect/n");   
  70.              }   
  71.          }           
  72.         private function closeHandler(event:Event):void  
  73.          {   
  74.               msg("closed.../n");   
  75.          }   
  76.          private function errorHandler(event:ErrorEvent):void {   
  77.              msg(event.text + "/n");   
  78.          }   
  79.         private function dataHandler(event:ProgressEvent):void {   
  80.              var n:int = socket.bytesAvailable;             
  81.             // Loop through each available byte returned from the socket connection.   
  82.             while (--n >= 0) {   
  83.                 // Read next available byte.   
  84.                  var b:int = socket.readUnsignedByte();                 
  85.                 switch (state) {   
  86.                     case 0:   
  87.                         // If the current byte is the "Interpret as Command" code, set the state to 1.   
  88.                         if (b == IAC) {   
  89.                              state = 1;   
  90.                         // Else, if the byte is not a carriage return, display the character using the msg() method.   
  91.                          } else if (b != CR) {   
  92.                              msg(String.fromCharCode(b));   
  93.                              }   
  94.                         break;   
  95.                     case 1:   
  96.                         // If the current byte is the "DO" code, set the state to 2.   
  97.                         if (b == DO) {   
  98.                              state = 2;   
  99.                          } else {   
  100.                              state = 0;   
  101.                          }   
  102.                         break;   
  103.                     // Blindly reject the option.   
  104.                     case 2:   
  105.                         /*
  106.                              Write the "Interpret as Command" code, "WONT" code,
  107.                              and current byte to the socket and send the contents
  108.                              to the server by calling the flush() method.
  109.                          */  
  110.                          socket.writeByte(IAC);   
  111.                          socket.writeByte(WONT);   
  112.                          socket.writeByte(b);   
  113.                          socket.flush();   
  114.                          state = 0;   
  115.                         break;   
  116.                  }   
  117.              }   
  118.          }   
  119.         private function msg(value:String):void {              
  120.              ta.text += value;   
  121.              ta.dispatchEvent(new Event(Event.CHANGE));   
  122.              setTimeout(setScroll, 100);   
  123.          }   
  124.          public function setScroll():void {   
  125.              ta.verticalScrollPosition = ta.maxVerticalScrollPosition;   
  126.          }          
  127.      }   
  128.        
  129. }  
  130. 这个as有的地方没有理解:初始化定义常量时,不知道为什么定义?希望有人能够解释下,小弟不胜感激。

    下面还有个Main .as

    1. package   
    2. {   
    3.     import flash.display.Sprite;   
    4.     import flash.events.MouseEvent;   
    5.     import flash.utils.ByteArray;   
    6.     import CustomSocket;   
    7.        
    8.     public class Main extends Sprite   
    9.      {      
    10.         private var telnetClient:CustomSocket;   
    11.   
    12.         public function Main() {   
    13.              setupUI();   
    14.          }   
    15.         private function connect(e:MouseEvent):void {   
    16.             //建立socket通信的链接   
    17.              telnetClient = new CustomSocket(serverName.text, int(portNumber.text), output);   
    18.          }   
    19.         private function sendCommand(e:MouseEvent):void {              
    20.              var ba:ByteArray = new ByteArray();   
    21.             //将得到的信息写入ba中   
    22.              ba.writeMultiByte(command.text + "/n", "GBK");     
    23.             //通过连接写入socket中   
    24.              telnetClient.writeBytesToSocket(ba);   
    25.              command.text = "";   
    26.          }   
    27.         private function setupUI():void {   
    28.              loginBtn.addEventListener(MouseEvent.CLICK,connect)   
    29.              sendBtn.addEventListener(MouseEvent.CLICK,sendCommand);   
    30.          }      
    31.      }   
    32. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值