as3如何使用settimeout解决自动重新连接?

时间:2017-08-21 10:06:21

标签: arrays sockets actionscript-3

我想从我的矢量列表中连接我的网址和端口。我在我的重新连接函数中使用setTimeout但我的问题是当我使用settimeout并单击重新连接按钮时settimout要求5次连接,并且当连接断开后连接成功因为settimeout继续发送请求我的列表连接和状态被破坏。

我如何解决这个问题或者有什么不同的方法?

public static function reconnect(serverUrl:String = "", serverPort:int = 0):void {

            var nextUri: SocketConnection = urisToTry.next;

            test = setTimeout(reconnect, 4000, nextUri.host, nextUri.port);


    }

这是我的迭代课

public class UriIterator 
{
     public var _availableAddresses: Vector.<SocketConnection> = new Vector.<SocketConnection>();
     public var currentIndex:int = 0;

    public function UriIterator(){

    }


    public function withAddress(host: String, port: int): UriIterator {
        const a: SocketConnection = new SocketConnection(host, port);
        _availableAddresses.push(a);
        return this;
    }


    public function get next():SocketConnection {
        var address = _availableAddresses[currentIndex];
        currentIndex++;
        if (currentIndex > _availableAddresses.length - 1)
            currentIndex = 0;
        return address;
    }




}

我的清单

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)

...

1 个答案:

答案 0 :(得分:2)

首先,简化并清理你所拥有的东西。

public class UriIterator 
{
    // You won't gain literally anything from using strong-typed Vector.
    // Long variable names make your code harder to read. Use simple ones.
    // Another good idea is to make these members private
    // so no one else will mess with them from the outside. 
    private var list:Aray = new Array;
    private var index:int = 0;

    // If constructor does nothing you might as well omit it.

    // It is not prohibited to do it the way you did,
    // yet again no gain for unnecessary complications.
    public function append(host:String, port:int):void
    {
        list.push(new SocketConnection(host, port));
    }

    public function get next():SocketConnection
    {
        var result:SocketConnection = list[index];
        if (++index >= list.length) index = 0;

        return result;
    }
}

然后

private var uriList:UriIterator;

uriList = new UriIterator;
uriList.append("http://urlone.com", 1211);
uriList.append("http://urltwo.com", 1212);
uriList.append("http://urlthree.com", 1213);
uriList.append("http://urlfour.com", 1214);

最后。

private var recId:uint;

// Event handler for refused connection or disconnected socket.
private function onDisconnect(e:Event):void
{
    // Dispose of existing/failed connection objects first.
    // @TODO

    // Just in case.
    clearTimeout(recId);

    // Now, lets schedule the next connection in 4 seconds.
    recId = setTimeout(onReconnect, 4000);
}

private function onReconnect():void
{
    // Get the next connection.
    var aNext:SocketConnection = uriList.next;

    // Put your connection routines here.
    // @TODO
}