Friday, April 12, 2013

RTMP Fallback Machanism

In Flex application some security reasons in the netowrk RTMP is blocked in the enterprise environment, so we unable to connect to the media streaming servers like wowza,flash media server and etc., RTMPT  will provide solution to the above. It provides the wrapper on top of RTMP and send the data through HTTP protocol and it through proxy. RTMP will run on 1935(default) and RTMPT  will run on (80). So defaultly port 80 is opened for all the proxy/N/W fire wall.But performance of RTMPT is lesser than the RTMP.

1)ProtocolUtilities.as


package com.asfusion.sample.model.utils
{
import com.asfusion.sample.model.vos.AppConfig;
import flash.events.AsyncErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.net.NetConnection;
import mx.controls.Alert;

public class ProtocolUtilities
{
private var counter:Number = 0;
[Bindable]
public var isRtmp:Boolean = true;
[Bindable]
public var protocolStatus:String = "";
private static var instance:ProtocolUtilities;
private var  _ncon:NetConnection;
public function ProtocolUtilities() {
}
public static function getInstance ():ProtocolUtilities {
if (!instance)
instance = new ProtocolUtilities();
return instance;
}
public function testConnection():void{
counter ++;
trace("Iniside of testConnection");
_ncon = new NetConnection();
_ncon.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
_ncon.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityError);
_ncon.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncError);
trace("IP :"+AppConfig.getInstance().appliation_IP);
if(counter == 1){
if(AppConfig.getInstance().debug == "true")
Alert.show(counter +" : rtmp");
_ncon.connect("rtmp://127.0.0.1:1935/vod");
if(AppConfig.getInstance().debug == "true")
Alert.show("rtmp://127.0.0.1:1935/vod");
}else{
if(AppConfig.getInstance().debug == "true")
Alert.show(counter +" : rtmpt");
_ncon.connect("rtmpt://127.0.0.1:80/vod");
if(AppConfig.getInstance().debug == "true")
Alert.show("rtmpt://127.0.0.1:80/vod");
}
trace("Outside of testConnection"+counter);
}
/**
*  Method tracks the connection state 
**/
public  function onNetStatus(e:NetStatusEvent):void {
var code:String = e.info.code;
if(AppConfig.getInstance().debug == "true")
Alert.show("NetStatus c:##"+code);
try {
if(code.indexOf("Success") != -1){
//connected successfully
trace("Successfully connected...");
                                        /*do something here*/
} else if (code.indexOf("Closed") != -1 ){
trace("Connection closed By User");
}else if (code.indexOf("Failed") != -1 ){
trace("connection failed");
if(counter <= 1 ){
trace("isRtmp :"+isRtmp);
isRtmp = false;
testConnection();
}else{
trace("isRtmp :"+isRtmp);
protocolStatus = "Sorry!!";
trace("Falied to connect");
}
}else {
trace("Connection Failed ..");
trace("Connection closed Becuase of Server inActive state");
_ncon = null;
trace("Sorry!! Media server is Inactive please try again!!");
}
trace("Net Status :" + code + "\r");
}catch(e:Error){
if(AppConfig.getInstance().debug == "true")
Alert.show("Error occured in NetStatus :"+e.getStackTrace());
trace("Error occured in NetStatus :"+e.getStackTrace());
_ncon = null;
} finally {
//Dispose Objects
}
}
private function securityError(e:SecurityErrorEvent):void {
_ncon.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityError);
//What do we need to do in case of a security error?
//To be implemented
//Alert.show("Security Error: "+ e.text);
trace("Security Error ::"+e);
}
private function asyncError(e:AsyncErrorEvent):void {
//What do we need to in case if there is fault?
//To be implemented
_ncon.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncError);
trace("AsyncError ::"+e);
//Alert.show("Async Error: "+ e.text);
}
}
}

No comments:

Post a Comment