1

当 connect() 失败时,我试图 close() 本地连接。但是在关闭连接时它给了我'#2083 close failed because object is not connected'。

提前致谢。

try{
    connServer2Client.connect("_" + clientConnectionName);
}catch (error:ArgumentError) {
     trace("in catch connection established");
    connServer2Client.close();
    connServer2Client.connect("_" + clientConnectionName);
}
4

1 回答 1

0

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。

于 2012-08-09T23:39:35.200 回答