Fernando Florez has just shared some interesting classes for FMS on the Flash Media List. SThey are handy if you want to load balance FMS connections on the client side. Why would you do that? It's cheaper than doing it serverside and also you have more control to switch servers for new connected clients on the fly without restarts, let you work with different providers, etc.
Here's some sample code:
2import funciton.utils.DateUtils;
3import funciton.net.BaseConnector;
4import funciton.utils.PendingCall;
5
6var upstream1:UpstreamServer = new UpstreamServer("myhost1", "myDemoApp", 1935, "rtmp");
7
8var upstream2:UpstreamServer = new UpstreamServer("myhost2", "myDemoApp", 443, "rtmps"); // box with security certificate
9upstream2.down = true; // it is down for maintenance
10
11var upstream3:UpstreamServer = new UpstreamServer("myhostAtOutsourceCompany", "myDemoApp", 1935, "rtmp");
12upstream3.max_fails = 3; // if it fails for more than 3 times disable it from the pool
13upstream3.expires = DateUtils.HOUR_IN_MS; // if it reaches max_fails re-enable it on the pool after an hour
14upstream3.backup = true; // it's a backup server. Use it only if upstream1 fails
15
16var baseConnector:BaseConnector = new BaseConnector();
17var ncHandler:PendingCall = baseConnector.connect(new Vector([upstream1, upstream2, upstream3]);
18ncHandler.onResult = function(nc:NetConnection):void{
19trace ("connection ok");
20}
21ncHandler.onFault = function():void{
22trace ("connection failed");
23}
BaseConnector class will do all the pool handling/logic. The first successful connection is kept and the rest are closed / discarded. With upstreams declared you can implement round robin or client hash logic easily.
The code is on github and can be found here: http://github.com/funciton/funciton-aslib
Thanks Fernando, great stuff.
