ActionScript 3的HTTPTunnel的实现

ActionScript 3Adobe公司开发的用于编写Flash的脚本语言。Adobe新推出的Adobe FlexRich Internet Application开发平台同样支持Action ScriptActionScript编写的Flex Data Service提供了丰富的数据处理功能,也包括实现了通过建立HTTPChannel的数据实时更新功能,例如聊天室,股市行情等。本文将使用ActionScript 3.0编写HTTPTunnel Client取代Flex Data ServiceHTTPChannel 用开源的Java HTTPTunnel作为Server,实现数据实时更新。

 

1 架构

Flash

Web Browser

JHTTPTunnel

Server

 

3 HTTPHeader+Content Data

1 Post

2 Get

1. Flash客户端连接HTTP Server并向HTTP Server发送Post命令。

2. Flash客户端连接HTTP Server并向HTTP Server发送Get命令。

3. HTTP ServerFlash不断发送遵循HTTP协议的数据,直到Flash客户端发送Close命令关闭连接。

4Flash客户端解析接受到的数据并更新界面。

 ActionScript 3的HTTPTunnel的实现

2.实现

2.1 客户端

MXML-类似于XML语言,用于部署Flash界面,可被Flex SDK编译为Flash文件。

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="620" height="341"

                            creationComplete="Refresh()">

<mx:Script>

<![CDATA[

              import org.colimas.http.*;

             

              import mx.collections.*;

             

              [Bindable]

              public var initDG:ArrayCollection;

             

              var host:String="localhost";

              var hport:int=8888;         

              var jhtc:HttpTunnelClient=new HttpTunnelClient(host, hport);

             

              private var DGArray:Array = [

                            { corp:'IBM', last:79.99},

                            { corp:'MS', last:30.99}];

                           

              private function Refresh():void {

                            trace("start...");

           Data1.text="Started";

                            Data2.text="Yes!";

                            jhtc.setInBound(new InTunnelSocket());

                            jhtc.setOutBound(new OutTunnelSocket());

                            initDG=new ArrayCollection(DGArray);

                            jhtc.connect();

                            jhtc.Register(initDG);                    

                            jhtc.close();

                            var myTimer:Timer=new Timer(300);

                            myTimer.addEventListener("timer", timerHandler);

                            myTimer.start();

              }

    public function timerHandler(event:TimerEvent):void {

        initDG.refresh();

    }

 

]]>

</mx:Script>

              <mx:Text x="41" y="38" text="Text1" width="62" height="28" id="Data1"/>

              <mx:Text x="124" y="38" text="Text2" width="62" height="28" id="Data2"/>

              <mx:DataGrid x="39" y="86" width="542" editable="false" id="Stock" dataProvider="{initDG}">

                            <mx:columns>

                                          <mx:DataGridColumn headerText="Corp." dataField="corp"/>

                                          <mx:DataGridColumn headerText="Last" dataField="last"/>

                            </mx:columns>

              </mx:DataGrid>

</mx:Application>

 

界面显示如下:

 ActionScript 3的HTTPTunnel的实现

Refresh()函数实现数据刷新。org.colimas.http.HttpTunnelClient类用ActionScript语言编写,实现HTTPTunnel客户端,完成连接HTTPTunnel并接受数据任务。

 

org.colimas.http.HttpTunnelClient实现:

 

数据发送OutTunnel类的实现

 

 

数据发送InTunnel类的实现

 

其他

 

 

3 服务器端

服务器端为JHTTPTunnel,可在网上下载。

我把部分代码修改,并加入数据生成程序。

  void ok(MySocket mysocket, InputStream in, int l, String sid) throws IOException{

              //死循环

              for(;;){

                           

                            try {

                                          Thread.sleep(300);

                                          mysocket.println("HTTP/1.1 200 OK");

                                          mysocket.println("Last-Modified: Thu, 04 Oct 2001 14:09:23 GMT");

                                          if(sid!=null){

                                                        mysocket.println("x-SESSIONID: "+sid);

                                          }

                                          mysocket.println("Content-Length: "+l);

                                          mysocket.println("Connection: close");

                                          mysocket.println("Content-Type: text/html; charset=iso-8859-1");

              //数据生成                          SimularData.stock[0]=SimularData.simulateChange(SimularData.stock[0]);

                                          SimularData.stock[1]=SimularData.simulateChange(SimularData.stock[1]);

                                          String tmp=String.valueOf(SimularData.stock[0])+" "+String.valueOf(SimularData.stock[1]);

//发送

                                          mysocket.println(tmp);

                                          mysocket.flush();

                            } catch (InterruptedException e) {

                                          // TODO Auto-generated catch block

                                          e.printStackTrace();

                                          mysocket.close();

                                          return;

                            } catch (IOException e) {

                                          // TODO Auto-generated catch block

                                          e.printStackTrace();

                                          mysocket.close();

                                          return;

                            }

                           

              }

4.运行结果

运行HTTPTunnel Server后,再打开Flash,可以看到界面的数据在不断更新。IBM和微软的股票在不断下降,哈哈。

ActionScript 3的HTTPTunnel的实现

上一篇:激活,数据存储,吐司


下一篇:如何搭建本地SVN服务