[ActionScript 3.0] NetConnection建立客户端与服务器的双向连接

一个客户端与服务器之间的接口测试的工具

 

<?xml version="1.0" encoding="utf-8"?>
<!--- - - - - - - - - - - - - - - - - - - - - - - - -
* @author:Frost.Yen
* @E-mail:871979853@qq.com
* @create:2016-7-13上午12:10:20
- - - - - - - - - - - - - - - - - - - - - - - - - - -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"  minWidth="955" minHeight="600" width="800" height="600" applicationComplete="application1_applicationCompleteHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.FlexEvent;
            private var _netConnection:NetConnection;
            protected function button1_clickHandler(event:MouseEvent):void
            {
                if(request.text == ""){
                    Alert.show("请输入请求地址","温馨提示");
                    return;
                }
                if(action.text == ""){
                    Alert.show("请输入请求方法","温馨提示");
                    return;
                }
                resultText.text = "connecting...";
                initNetConnection();
                _netConnection.connect( request.text as String);
                _netConnection.call(action.text,new Responder(function(result:Object):void{
                    if (result) {
                        trace("onResult: ",JSON.stringify(result));
                        resultText.text = "onResult:\n\n"+"ation:"+action.text+"\narguments:"+param.text+"\n\nresult:\n"+JSON.stringify(result);
                    }
                }),param.text);
            }
            
            private function initNetConnection():void
            {
                if (!_netConnection) {
                    trace("initConnection");
                    _netConnection = new NetConnection();
                    NetConnection.defaultObjectEncoding = ObjectEncoding.AMF3;
                    _netConnection.addEventListener(NetStatusEvent.NET_STATUS,onStatus);
                }
            }
            public function onStatus(e:NetStatusEvent):void
            {
                trace("e.info.code", e.info.code);
                resultText.text = e.info.code;
                switch (e.info.code) {
                    case "NetConnection.Connect.Success":
                    case "NetConnection.Connect.Rejected":
                    case "NetConnection.Connect.NetworkChange":
                    case "NetConnection.Connect.Closed":
                        break;
                    case "NetConnection.Call.Failed":
                    case "NetConnection.Connect.Failed":
                        //case "NetConnection.Call.BadVersion"://以不能识别的格式编码的数据包。
                        trace("onReConnect");
                        if (_netConnection) {
                            _netConnection.removeEventListener(NetStatusEvent.NET_STATUS,onStatus);
                            _netConnection.close();
                            _netConnection = null;
                        }
                        break;
                }
            }
            private function test(...arg):void
            {
                trace(arg);
            }
            
            protected function application1_applicationCompleteHandler(event:FlexEvent):void
            {
                test("1",2,3);
            }
            
        ]]>
    </fx:Script>
    
    
    <s:HGroup gap="4" top="10" left="5"  >
        <s:Label text="RequestURL:" paddingTop="6"/>
        <s:TextInput id="request" width="180" text="http://45.63.127.27/web/"/>
        <s:Label text="action:"  paddingTop="6"/>
        <s:TextInput id="action" width="150" text="LoginAction.index"/>
        <s:Label text="params:"  paddingTop="6"/>
        <s:TextInput id="param" width="200" text="login,apple,tomyuang"/>
        <s:Button label="send" click="button1_clickHandler(event)"/>
    </s:HGroup>
    <s:TextArea id="resultText" width="790" height="500" left ="5" top="50" selectable="true" />
</s:Application>

 

posted on 2016-07-13 15:45  晏过留痕  阅读(1236)  评论(0编辑  收藏