使用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__
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="" layout="vertical" width="422" height="330">
- <mx:Script>
- <![CDATA[
- private var s:Socket;
-
- private function conn():void //conn,disconn,reconn
- {
- if (bt_conn.label == "disconn")
- {
- s.close(); //disconnect
- bt_conn.label="reconn";
- bt_send.enabled=false;
- } else {
- s=new Socket();
-
- //添加键盘事件侦听器捕获txi_send的键盘输入
- txi_send.addEventListener(KeyboardEvent.KEY_DOWN,txi_sendKeyDown);
-
- //添加socket事件侦听器
- if (s.hasEventListener(Event.CONNECT) == false)
- {
- s.addEventListener(Event.CONNECT,firstConn);
- }
- if (s.hasEventListener(Event.CLOSE) == false)
- {
- s.addEventListener(Event.CLOSE,closeConn);
- }
- if (s.hasEventListener(ProgressEvent.SOCKET_DATA) == false)
- {
- s.addEventListener(ProgressEvent.SOCKET_DATA,recvData);
- }
- if (s.hasEventListener(IOErrorEvent.IO_ERROR) == false)
- {
- s.addEventListener(IOErrorEvent.IO_ERROR,ioErr);
- }
- if (s.hasEventListener(IOErrorEvent.NETWORK_ERROR) == false)
- {
- s.addEventListener(IOErrorEvent.NETWORK_ERROR,networkErr);
- }
-
- s.connect(txi_ip.text, uint(txi_port.text)); //connect
- bt_conn.label="disconn";
- bt_send.enabled=true;
- }
- }
-
- private function firstConn(e:Event):void //首次连接成功后调用
- {
- trace("Sucess connect to server " + txi_ip.text + ":" + txi_port.text);
- txa_trace.text += "Sucess connect to server " + txi_ip.text + ":" + txi_port.text + "\n";
- s.writeUTFBytes("I am comming!\n");
- s.flush();
- txa_trace.text += "Send> " + "I am comming!\n";
- }
-
- private function closeConn(e:Event):void //连接被断开
- {
- //trace("Disconn conn!");
- txa_trace.text += "Disconn conn!\n";
- bt_conn.label="reconn";
- bt_send.enabled=false;
- }
-
- private function recvData(e:Event):void //socket中有数据
- {
- var recvBuf:uint = s.bytesAvailable;
- var r:String;
- if (recvBuf > 1024)
- {
- r=s.readUTFBytes(1024);
- txa_trace.text += "开始接收数据,接收长度为:" + r.length + "\n";
- //trace("Recv> " + r + " (数据未接收完)");
- txa_trace.text += "Recv> " + r + " (数据未接收完)" + "\n";
- } else {
- r=s.readUTFBytes(recvBuf);
- //trace("Recv> " + r + " (数据已接收完)");
- txa_trace.text += "Recv> " + r + " (数据已接收完)" + "\n";
- }
- }
-
- private function ioErr(e:Event):void //io错误
- {
- //trace("IO err, disconn!");
- txa_trace.text += "IO err, disconn!\n";
- s.close();
- bt_conn.label="reconn";
- bt_send.enabled=false;
- }
-
- private function networkErr(e:Event):void //network错误
- {
- //trace("Network err, disconn!");
- txa_trace.text += "Network err, disconn!\n";
- s.close();
- bt_conn.label="reconn";
- bt_send.enabled=false;
- }
-
- private function sendMsg():void //发送消息给服务端
- {
- var str:String=txi_send.text;
- //trace("Send> " + str);
- txi_send.text="";
- s.writeUTFBytes(str + "\n");
- s.flush();
- txa_trace.text += "Send> " + str + "\n";
- }
-
- private function txi_sendKeyDown(e:KeyboardEvent):void //txi_send 有键盘输入
- {
- if (e.keyCode == Keyboard.ENTER && bt_send.enabled == true) //输入为回车且发送按钮状态为可用
- {
- sendMsg();
- }
- }
-
- ]]>
- </mx:Script>
- <mx:HBox width="364">
- <mx:Label text="ip"/>
- <mx:TextInput id="txi_ip" text="192.168.56.101"/>
- <mx:Label text="Port"/>
- <mx:TextInput width="55" id="txi_port" text="10001"/>
- <mx:Button label="Connect" id="bt_conn" click="{conn()}"/>
- </mx:HBox>
- <mx:Panel width="368" height="223" layout="absolute" title="Messages" cornerRadius="15">
- <mx:Canvas x="10" y="10" width="338" height="163">
- <mx:TextArea width="328" height="163" id="txa_trace" wordWrap="true" x="0" y="0" editable="false"/>
- </mx:Canvas>
- </mx:Panel>
- <mx:HBox width="342">
- <mx:TextInput width="261" id="txi_send"/>
- <mx:Button label="Send" width="72" click="{sendMsg()}" id="bt_send" enabled="false"/>
- </mx:HBox>
-
- </mx:Application>
阅读(1568) | 评论(0) | 转发(1) |