当连接失败时我有connectAgain功能i addchild按钮,当我点击这个按钮时,调用此函数中的connectAgain函数有serverUrl和serverPort只有一个ip和端口这是当前系统及其工作。
但是现在,我有4或5个ip和端口。我想我添加数组或字典或对象,当连接失败时我点击按钮并调用阵列内的IP和端口,但每个连接丢失我想调用下一个IP和端口不是相同的IP和端口。
请帮助我,谢谢这是我目前的功能。我该怎么做 ?
public static function connectAgain(serverUrl:String = "", serverPort:int = 0):void {
serverUrl = myGameClass.serverUrl;
serverPort = myGameClass.serverPort;
}
答案 0 :(得分:1)
您可以使用以下辅助类:
class UriIterator {
private var _availableAddresses: Vector.<UriVO> = new Vector.<UriVO>();
public function withAddress(host: String, port: int): UriIterator {
const a: UriVO = new UriVO(host, port);
_availableAddresses.push(a);
return this;
}
public function get next(): UriVO
{
return _availableAddresses.length ? _availableAddresses.pop() : null;
}
}
class UriVO {
private var _host: String;
private var _port: int;
public function Address(host: String, port: int) {
_host = host;
_port = port;
}
public function get host():String {
return _host;
}
public function get port():int {
return _port;
}
}
在init的某个地方创建迭代器:
...
const urisToTry: UriIterator = new UriIterator()
.withAddress("http://urlone.com", 1211)
.withAddress("http://urltwo.com", 1212)
.withAddress("http://urlthree.com", 1213)
.withAddress("http://urlfour.com", 1214)
...
然后,在重新连接功能中,您可以调用next()
函数来检索下一个连接URL:
const nextUri: UriVO = urisToTry.next;
if (nextUri)
connectAgain(nextUri.host, nextUri.port);
else
// you've tried all uris and connection failed.