实现ActionScript 3的HTTPTunnel的步骤

实现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实现:

package org.colimas.http
{
 import flash.utils.ByteArray;
 import flash.errors.IOError;
 import mx.collections.ArrayCollection;
 
public class HttpTunnelClient extends HttpTunnel
 {
 static private var CONTENT_LENGTH:int=1024*10;
 
private var init:Boolean=false;
 private var closed:Boolean=false;
 
private var dest_host:String=null;
 private var dest_port:int=0;
 private var proxy:Proxy=null;
 
private var ib:InTunnel=null;
 private var ob:OutTunnel=null;
 
public function HttpTunnelClient( host:String, port:int){
 this.dest_host=host;
 this.dest_port=port;
 }
 /*传入用于界面显示的数据源*/
 public function Register(DGArray:ArrayCollection):void{
 this.ib.setData(DGArray);
 }
 
public function setProxy( host:String, port:int):void{
 this.proxy=new Proxy(host, port);
 }
 
public function connect():void{
 
if(ib==null){
 trace("InTunnel is not given");
 return;
 }
 ib.setHost(dest_host);
 ib.setPort(dest_port);
 ib.setProxy(proxy);
 
if(ob==null){
 trace("OutTunnel is not given");
 return;
 }
 ob.setHost(dest_host);
 ob.setPort(dest_port);
 ob.setProxy(proxy);
 ob.setContentLength(CONTENT_LENGTH);
 
getOutbound();
 getInbound();
 }
 /*数据发送OutTunnel类连接服务器端*/
 private function getOutbound():void{
 if(closed){
 trace("broken pipe"