Chinaunix首页 | 论坛 | 博客
  • 博客访问: 599932
  • 博文数量: 60
  • 博客积分: 3993
  • 博客等级: 中校
  • 技术积分: 1572
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 17:08
文章分类

全部博文(60)

文章存档

2012年(7)

2011年(35)

2010年(8)

2009年(7)

2008年(3)

分类: Python/Ruby

2011-02-11 21:46:34

使用perl socket想法错误的理解AS socket,应完全使用Event事件!此文错误,作废作废!
__DELETE__
原先期望flex能够直接调用原来一直在用的perl xml-rpc,看了flex的数据源要求后才知道不行。
通过php连接数据库的又修改起来太繁琐,而且php也不太了解,也不想从php取数据。
又想用flex调用perl的soap webservice,用perl的soap客户端测试都没问题,但到了flex这边
怎么也读不了wsdl,没有弄明白为什么,只能尝试下先用socket,soap等有空了再研究吧。。
呵呵,用AS的socket有点惊喜,没想到和Perl一样方便,而且作为客户端要写的东西也很少。
等把socket部分熟悉下再来修改这个脚本~
__cut__

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="" layout="vertical" width="422" height="330">
  3.     <mx:Script>
  4.         <![CDATA[
  5.             private var s:Socket;
  6.             
  7.             private function conn():void //conn,disconn,reconn
  8.             {
  9.                 if (bt_conn.label == "disconn")
  10.                 {
  11.                         s.close(); //disconnect
  12.                         bt_conn.label="reconn";
  13.                         bt_send.enabled=false;
  14.                 } else {
  15.                     s=new Socket();
  16.                     
  17.                     //添加键盘事件侦听器捕获txi_send的键盘输入
  18.                     txi_send.addEventListener(KeyboardEvent.KEY_DOWN,txi_sendKeyDown);
  19.                     
  20.                     //添加socket事件侦听器
  21.                     if (s.hasEventListener(Event.CONNECT) == false)
  22.                     {
  23.                         s.addEventListener(Event.CONNECT,firstConn);
  24.                     }
  25.                     if (s.hasEventListener(Event.CLOSE) == false)
  26.                     {
  27.                         s.addEventListener(Event.CLOSE,closeConn);
  28.                     }
  29.                     if (s.hasEventListener(ProgressEvent.SOCKET_DATA) == false)
  30.                     {
  31.                         s.addEventListener(ProgressEvent.SOCKET_DATA,recvData);
  32.                     }
  33.                     if (s.hasEventListener(IOErrorEvent.IO_ERROR) == false)
  34.                     {
  35.                         s.addEventListener(IOErrorEvent.IO_ERROR,ioErr);
  36.                     }
  37.                     if (s.hasEventListener(IOErrorEvent.NETWORK_ERROR) == false)
  38.                     {
  39.                         s.addEventListener(IOErrorEvent.NETWORK_ERROR,networkErr);
  40.                     }
  41.                     
  42.                     s.connect(txi_ip.text, uint(txi_port.text)); //connect
  43.                     bt_conn.label="disconn";
  44.                     bt_send.enabled=true;
  45.                 }
  46.             }
  47.             
  48.             private function firstConn(e:Event):void //首次连接成功后调用
  49.             {
  50.                 trace("Sucess connect to server " + txi_ip.text + ":" + txi_port.text);    
  51.                 txa_trace.text += "Sucess connect to server " + txi_ip.text + ":" + txi_port.text + "\n";
  52.                 s.writeUTFBytes("I am comming!\n");
  53.                 s.flush();
  54.                 txa_trace.text += "Send> " + "I am comming!\n";
  55.             }
  56.             
  57.             private function closeConn(e:Event):void //连接被断开
  58.             {
  59.                 //trace("Disconn conn!");
  60.                 txa_trace.text += "Disconn conn!\n";
  61.                 bt_conn.label="reconn";    
  62.                 bt_send.enabled=false;
  63.             }
  64.             
  65.             private function recvData(e:Event):void //socket中有数据
  66.             {
  67.                 var recvBuf:uint = s.bytesAvailable;
  68.                 var r:String;
  69.                 if (recvBuf > 1024)
  70.                 {
  71.                     r=s.readUTFBytes(1024);
  72.                     txa_trace.text += "开始接收数据,接收长度为:" + r.length + "\n";
  73.                     //trace("Recv> " + r + " (数据未接收完)");
  74.                     txa_trace.text += "Recv> " + r + " (数据未接收完)" + "\n";
  75.                 } else {
  76.                     r=s.readUTFBytes(recvBuf);
  77.                     //trace("Recv> " + r + " (数据已接收完)");
  78.                     txa_trace.text += "Recv> " + r + " (数据已接收完)" + "\n";
  79.                 }
  80.             }
  81.             
  82.             private function ioErr(e:Event):void //io错误
  83.             {
  84.                 //trace("IO err, disconn!");
  85.                 txa_trace.text += "IO err, disconn!\n";
  86.                 s.close();
  87.                 bt_conn.label="reconn";    
  88.                 bt_send.enabled=false;
  89.             }
  90.             
  91.             private function networkErr(e:Event):void //network错误
  92.             {
  93.                 //trace("Network err, disconn!");
  94.                 txa_trace.text += "Network err, disconn!\n";
  95.                 s.close();
  96.                 bt_conn.label="reconn";    
  97.                 bt_send.enabled=false;
  98.             }
  99.             
  100.             private function sendMsg():void //发送消息给服务端
  101.             {
  102.                 var str:String=txi_send.text;
  103.                 //trace("Send> " + str);
  104.                 txi_send.text="";
  105.                 s.writeUTFBytes(str + "\n");
  106.                 s.flush();
  107.                 txa_trace.text += "Send> " + str + "\n";
  108.             }
  109.             
  110.             private function txi_sendKeyDown(e:KeyboardEvent):void //txi_send 有键盘输入
  111.             {
  112.                 if (e.keyCode == Keyboard.ENTER && bt_send.enabled == true) //输入为回车且发送按钮状态为可用
  113.                 {
  114.                     sendMsg();
  115.                 }
  116.             }
  117.             
  118.         ]]>
  119.     </mx:Script>
  120.     <mx:HBox width="364">
  121.         <mx:Label text="ip"/>
  122.         <mx:TextInput id="txi_ip" text="192.168.56.101"/>
  123.         <mx:Label text="Port"/>
  124.         <mx:TextInput width="55" id="txi_port" text="10001"/>
  125.         <mx:Button label="Connect" id="bt_conn" click="{conn()}"/>
  126.     </mx:HBox>
  127.     <mx:Panel width="368" height="223" layout="absolute" title="Messages" cornerRadius="15">
  128.         <mx:Canvas x="10" y="10" width="338" height="163">
  129.             <mx:TextArea width="328" height="163" id="txa_trace" wordWrap="true" x="0" y="0" editable="false"/>
  130.         </mx:Canvas>
  131.     </mx:Panel>
  132.     <mx:HBox width="342">
  133.         <mx:TextInput width="261" id="txi_send"/>
  134.         <mx:Button label="Send" width="72" click="{sendMsg()}" id="bt_send" enabled="false"/>
  135.     </mx:HBox>
  136.     
  137. </mx:Application>
阅读(1568) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~