本连接器代码是可支持Flashcom多端口(1935,443,80),多协议(rtmp,rtmps,rtmpt)扫描.
以下是Connector.as代码片段:
//======================================;
//Name:Connector.as
//Auther:Kinglong
//Email:kinglong@gmail.com
//Date:2005-06-29
//Desc:FCS连接器;
//=======================================;
importmx.events.EventDispatcher;
//=======================================;
classcom.klstudio.fcs.ConnectorextendsObject{
//======================================;
privatevardispatchEvent:Function;
publicvaraddEventListener:Function;
publicvarremoveEventListener:Function;
publicvartarget:Object;
publicvarindex:Number;
//======================================;
privatevar__connectListArray:Array;
privatevar__host_name:String;
privatevar__app_name:String;
privatevar__connected:Boolean;
privatevar__connect_list_point:Number;
privatevar__connection_num:Number;
privatevar__connection_max_num:Number;
privatevar__connection:NetConnection;
privatevar__connection_params:Object;
//=======================================;
privatevar__mar_next_connect:Number;
privatevar__mar_all_failed_connect:Number;
//=======================================;
functionConnector(host:String,app:String){
if(arguments.length>1){
this.__host_name=host;
this.__app_name=app;
}
this.initConnector();
}
//========================================;
//初始化对象;
//========================================;
privatefunctioninitConnector():Void{
//=======================================;
EventDispatcher.initialize(this);
//=======================================;
this.__connected=false;
this.__connection_num=0;
this.__connection_max_num=3;
this.__connection=null;
this.__connection_params=null;
//=======================================;
this.__connectListArray=newArray();
this.__connectListArray.push({protocol:"rtmp",port:1935});
this.__connectListArray.push({protocol:"rtmp",port:443});
this.__connectListArray.push({protocol:"rtmpt",port:80});
this.__connectListArray.push({protocol:"rtmps",port:443});
//=======================================;
}
//========================================;
//启动连接表;
//========================================;
privatefunctiondoActualConnect():Void{
this.removeAllTimeOut();
this.__connection_num++;
if(this.__connection_num>this.__connection_max_num){
this.myTrace("总共尝试"+this.__connection_max_num+"次都失败!");
this.dispatchEvent({type:"failed",data:"服务器连接失败!"});
return;
}
this.__connect_list_point=0;
vari:Number;
for(i=0;i<this.__connectListArray.length;i++){
this["temp_nc_"+i]=newNetConnection();
this["temp_nc_"+i].index=i;
this["temp_nc_"+i].target=this;
this["temp_nc_"+i].onStatus=this.doStatus;
}
varconnection_uri:String=this.__connectListArray[this.__connect_list_point].protocol+"://"+this.__host_name+":"+this.__connectListArray[this.__connect_list_point].port+"/"+this.__app_name;
this.myTrace("[尝试"+this.__connection_num+"/"+this.__connection_max_num+"]连接到"+(this.__connect_list_point+1)+"/"+this.__connectListArray.length+":"+connection_uri);
this.dispatchEvent({type:"connect",data:this.__connectListArray[this.__connect_list_point]});
this["temp_nc_"+this.__connect_list_point].connect(connection_uri,this.__connection_params);
this.__mar_next_connect=setInterval(this,"nextConnect",3000);
}
//========================================;
//下一个连接;
//========================================;
privatefunctionnextConnect():Void{
clearInterval(this.__mar_next_connect);
this.__connect_list_point++;
if(this.__connect_list_point<this.__connectListArray.length){
varconnection_uri:String=this.__connectListArray[this.__connect_list_point].protocol+"://"+this.__host_name+":"+this.__connectListArray[this.__connect_list_point].port+"/"+this.__app_name;
this.myTrace("[尝试"+this.__connection_num+"/"+this.__connection_max_num+"]连接到"+(this.__connect_list_point+1)+"/"+this.__connectListArray.length+":"+connection_uri);
this.dispatchEvent({type:"connect",data:this.__connectListArray[this.__connect_list_point]});
this["temp_nc_"+this.__connect_list_point].connect(connection_uri,this.__connection_params);
this.__mar_next_connect=setInterval(this,"nextConnect",3000);
}
}
//=========================================;
//下一个连接;
//=========================================;
privatefunctionallFailedConnect():Void{
clearInterval(this.__mar_all_failed_connect);
this.doActualConnect();
}
//=========================================;
//取消所有线程;
//=========================================;
privatefunctionremoveAllTimeOut(ncIndex:Number):Void{
if(arguments.length==0){
ncIndex=-1;
}
clearInterval(this.__mar_next_connect);
clearInterval(this.__mar_all_failed_connect);
vari:Number;
for(i=0;i<this.__connectListArray.length;i++){
if(i==ncIndex){
this.myTrace(this.__connectListArray[ncIndex].protocol+"://"+this.__host_name+":"+this.__connectListArray[ncIndex].port+"/"+this.__app_name+"连接尝试成功!");
}else{
this["temp_nc_"+i].close();
deletethis["temp_nc_"+i];
}
}
}
//=========================================;
privatefunctiondoStatus(ncInfo:Object):Void{
this.target.onStatus(this.index,ncInfo);
}
//=========================================;
privatefunctiononStatus(ncIndex:Number,ncInfo:Object):Void{
switch(ncInfo.code){
case"NetConnection.Connect.Success":
this.removeAllTimeOut(ncIndex);
this.__connection=this["temp_nc_"+ncIndex];
this.__connected=true;
this.dispatchEvent({type:"success"});
break;
case"NetConnection.Connect.Failed":
this.myTrace(this.__connectListArray[ncIndex].protocol+"://"+this.__host_name+":"+this.__connectListArray[ncIndex].port+"/"+this.__app_name+"连接尝试失败!");
if(ncIndex==this.__connectListArray.length-1){
this.myTrace("所有连接尝试失败!");
this.removeAllTimeOut();
this.__mar_all_failed_connect=setInterval(this,"allFailedConnect",5000);
}
break;
case"NetConnection.Connect.Closed":
case"NetConnection.Connect.AppShutdown":
this.dispatchEvent({type:"closed"});
break;
case"NetConnection.Connect.Rejected":
this.removeAllTimeOut();
this.myTrace("FlashCom服务器不接收连接尝试!");
this.dispatchEvent({type:"failed",data:ncInfo.application.message});
break;
}
}
//=========================================;
//调试;
//=========================================;
privatefunctionmyTrace(msg:Object):Void{
trace("#Connector#:"+msg);
getURL("FSCommand:log","#Connector#:"+msg);
}
//=========================================;
//设置连接参数;
//=========================================;
publicfunctionsetConnectParams(param:Object):Void{
this.__connection_params=param;
}
//=========================================;
//设置连接表;
//=========================================;
publicfunctionsetConnectListArray(arr:Array):Void{
this.__connectListArray=arr;
}
//=========================================;
//设置最大尝试连接资料;
//=========================================;
publicfunctionsetConnectMaxNum(num:Number):Void{
if(num>1&&num<11){
this.__connection_max_num=num;
}
}
//=========================================;
//连接FCS服务器;
//=========================================;
publicfunctionconnect():Void{
if(this.__connected)return;
this.__connection_num=0;
this.doActualConnect();
}
//=========================================;
//检查是否连接成功;
//=========================================;
publicfunctionisConnected():Boolean{
returnthis.__connected;
}
//=========================================;
//获取主连接NC;
//=========================================;
publicfunctiongetConnection():NetConnection{
returnthis.__connection;
}
}