List Info

Thread: Updated connect helper class for flex




Updated connect helper class for flex
country flaguser name
Australia
2007-04-29 23:10:06
Hi ive updated my flex connect helper class to add
functionality for 
AMF0 and AMF3, and fallback connections. Ive now set AMF3 as
default as 
red5 can handle that now, and updated the code to add a list
of urls to 
attempt to connect to which may be useful when the main host
is a load 
balancer and the other urls can be the public ip addresses
to the 
machines just in case the load balancer isnt working
properly.

The other discovery is that fallback in netconnection isnt
on by 
default, so have to add nc.proxyType="best", that
will scan rtmpt and 
rtmps on the first host. I turn proxytype back off when
reconnecting so 
have to add rtmpt urls for the other hosts.

usage

nc:Connect = new Connect();

or  for AMF0

nc:Connect = new Connect(true);

nc.addConnection("rtmp://otherhost/app");
nc.addConnection("rtmpt://otherhost/app");
nc.addConnection("rtmp://anotherhost/app");
nc.addConnection("rtmpt://anotherhost/app");

package com.domain.net
{
    import flash.net.NetConnection;
    import flash.events.NetStatusEvent;
    import flash.events.EventDispatcher;
    import mx.utils.StringUtil;
    import flash.utils.setInterval;
    import flash.utils.setTimeout;
    import flash.utils.clearInterval;
    import flash.utils.clearTimeout;
    import com.domain.ClientInfo;
    import flash.events.Event;
    import com.domain.events.ConnectEvent;
    import flash.net.ObjectEncoding;
    import mx.collections.ArrayCollection;
    import mx.logging.ILogger;
    import mx.logging.Log;

    [Event("connected")]
    [Event("reconnected")]
    [Event("failed")]
    [Event("disconnected")]
   
   
   
    public class Connect extends NetConnection
    {
        private var code:String;
        private var connectRetry:int = 0;
        private var connectRetryTimeout:int = 1500;
        public var ncInt:Array = new Array();
        private var connections:Array = new Array();
        private var _log:ILogger =
Log.getLogger("Connect");
       
        public function Connect(encodeAMF0:Boolean = false)
        {
            setDefaultEncoding(encodeAMF0);
            super();
            this.proxyType = "best";
            this.addEventListener(NetStatusEvent.NET_STATUS,
onStatus);
            connections.push("");
        }
       
        public function get log():ILogger
        {
            return _log;   
        }
       
        public function addConnection(host:String):void
        {
            connections.push(host);
        }
       
   
        override public function connect(command:String, ...
rest):void
        {

   
            switch (rest.length)
            {
                case 0:
                    super.connect(command);
                    break;
                case 1:
                    super.connect(command, rest[0]);
                    break;
                case 2:
                    super.connect(command, rest[0],
rest[1]);
                    break;
                case 3:
                    super.connect(command, rest[0], rest[1],
rest[2]);
                    break;
            }
        }
       
       
        private function
setDefaultEncoding(encodeAMF0:Boolean = false):void
        {
            if (encodeAMF0)
            {
                this.objectEncoding =
flash.net.ObjectEncoding.AMF0;
            } else {
                this.objectEncoding =
flash.net.ObjectEncoding.DEFAULT;
            }
        }
   
       
        public function onStatus(event:NetStatusEvent):void
        {
            switch (event.info.code)
            {
                case
"NetConnection.Connect.Success":
                    this.dispatchEvent(new 
ConnectEvent(ConnectEvent.CONNECTED,false,false,ConnectEvent
.CONNECTED_MSG));
                    stopReconnect();
                break;
                case
"NetConnection.Connect.Failed":
                    nextConnect();
                break;
                case
"NetConnection.Connect.Closed":
                    this.dispatchEvent(new 
ConnectEvent(ConnectEvent.DISCONNECTED,false,false,ConnectEv
ent.DISCONNECTED_MSG));
                break;
            }
           
        }
       
        private function stopReconnect():void
        {
            clearTimeout(this.ncInt[0]);   
        }
       
        private function nextConnect():void
        {
            log.info("Couldnt connect to " +
this.uri);
            this.ncInt[0] = setTimeout(tryFallBack,
connectRetryTimeout);
        }
       
        private function tryFallBack():void
        {
            connectRetry++;
            this.proxyType = "none";
           
            if (connectRetry >= connections.length)
            {   
                stopReconnect();
                this.dispatchEvent(new 
ConnectEvent(ConnectEvent.FAILED,false,false,ConnectEvent.FA
ILED_MSG));
            } else {
                this.dispatchEvent(new 
ConnectEvent(ConnectEvent.RECONNECTED,false,false,ConnectEve
nt.RECONNECTED_MSG));
                log.info("Attempting connection to
" + 
connections[connectRetry]);
                this.close();
                this.connect(connections[connectRetry]);
            }
        }
       
    }
}



_______________________________________________
Red5 mailing list
Red5osflash.org

http://osflash.org/mailman/listinfo/red5_osflash.org

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )