Adobe 真的搞砸了 IMO。
应该有一个要检查的参数或要调用的函数,它会告诉您状态。IE
connected = true or false
但是没有。所以最好的解决方案是多个 try catch 函数,这很糟糕!这是我使用的课程。我将 flex 与 html/javascript 一起使用,所以这是在 javascript 中,但您可以在 as3 中执行类似的操作。
function Connecter(){
this.LocalConnection = new LocalConnection();
this.LocalConnection.allowDomain("*");
this.connectionAttempts = 0;
this.localConnectionError = false;
this.connected = false;
}
Connecter.prototype.openConnection = function(channel){
try{
this.LocalConnection.connect(channel); //will throw error if connection is opened
} catch(e){
this.closeConnection();
this.LocalConnection.connect(channel); //try to reconnect again
}
this.connected = true;
};
Connecter.prototype.closeConnection = function(channel){
try{
this.LocalConnection.close(); //will throw error if connection is closed
} catch(e){ /* Already closed */ }
this.connected = false;
};
Connecter.prototype.connect = function(channel) {
var self = this;
while (self.connectionAttempts < 3 && !self.connected ) {
self.connectionAttempts += 1;
self.openConnection(channel);
}
};
因此,已连接将帮助告诉您是否有 LocalConnection。