本连接器代码是可支持Flashcom多端口(1935,443,80),多协议(rtmp,rtmps,rtmpt)扫描.
以下是Connector.as代码片段:
//======================================;
// Name:Connector.as
// Auther:Kinglong
// Email:kinglong@gmail.com
// Date:2005-06-29
// Desc:FCS连接器;
//=======================================;
import mx.events.EventDispatcher;
//=======================================;
class com.klstudio.fcs.Connector extends Object{
//======================================;
private var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;
public var target:Object;
public var index:Number;
//======================================;
private var __connectListArray:Array;
private var __host_name:String;
private var __app_name:String;
private var __connected:Boolean;
private var __connect_list_point:Number;
private var __connection_num:Number;
private var __connection_max_num:Number;
private var __connection:NetConnection;
private var __connection_params:Object;
//=======================================;
private var __mar_next_connect:Number;
private var __mar_all_failed_connect:Number;
//=======================================;
function Connector(host:String,app:String){
if(arguments.length>1){
this.__host_name = host;
this.__app_name = app;
}
this.initConnector();
}
//========================================;
// 初始化对象;
//========================================;
private function initConnector():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 = new Array();
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});
//=======================================;
}
//========================================;
// 启动连接表;
//========================================;
private function doActualConnect():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;
var i:Number;
for(i=0;i<this.__connectListArray.length;i++){
this["temp_nc_"+i] = new NetConnection();
this["temp_nc_"+i].index = i;
this["temp_nc_"+i].target = this;
this["temp_nc_"+i].onStatus = this.doStatus;
}
var connection_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);
}
//========================================;
// 下一个连接;
//========================================;
private function nextConnect():Void{
clearInterval(this.__mar_next_connect);
this.__connect_list_point ++;
if(this.__connect_list_point<this.__connectListArray.length){
var connection_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);
}
}
//=========================================;
// 下一个连接;
//=========================================;
private function allFailedConnect():Void{
clearInterval(this.__mar_all_failed_connect);
this.doActualConnect();
}
//=========================================;
// 取消所有线程;
//=========================================;
private function removeAllTimeOut(ncIndex:Number):Void{
if(arguments.length == 0){
ncIndex = -1;
}
clearInterval(this.__mar_next_connect);
clearInterval(this.__mar_all_failed_connect);
var i: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();
delete this["temp_nc_"+i];&nb