21- Create Client App to Access Red5 Server App

  1. Create a new fla file, and with frame 1 selected, open the Actions panel and type in the following code:
    import flash.net.NetConnection;
    import flash.net.Responder;
    
    var nc:NetConnection = new NetConnection();
    nc.connect("rtmp://localhost/testapp");
    nc.addEventListener(NetStatusEvent.NET_STATUS, netConnectionHandler);
    nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
    
    var nr:Responder = new Responder(netResponderHandler);
    nc.call("add", nr, 2, 3);
    
    function netConnectionHandler(evt:NetStatusEvent)
    {
    	trace("status: " + evt.info.code);
    }
    
    function securityErrorHandler(evt:SecurityErrorEvent):void 
    {
    	trace("securityErrorHandler: " + evt);
    }
    
    function netResponderHandler(serverResult:Object)
    {
    	trace("The result is " + serverResult);
    }
    
  2. First we import the namespaces flash.net.NetConnection and flash.net.Responder, then we create a new NetConnection object, and then we connect to the server app using the path rtmp://localhost/testapp (where testapp is the name of the contextPath inside red-web.properties of the server app). Then we add an event listener, NET_STATUS, that listens for status information of the connection (like Success, Failure, or InvalidApp. Then we add an event listener SECURITY_ERROR to listen for security errors.

    Next we want to call the add() function on the server app, so we use this syntax:

    var nr:Responder = new Responder(netResponderHandler);
    nc.call("add", nr, 2, 3);


    We use nc.call("add", nr, 2, 3) to call the server app function, and we pass it the nr responder object. That object is created and passed an event handler called netResponderHandler() which will recieve the results of the add() function, as it's parameter. If we pass 2 and 3 to the add() function then netResponderHandler() will receive 5 as its serverResult parameter.
  3. RUN THE FLA FILE: Click on Control in the top nav, then click Test Movie, and in the console the output should be:

    status: NetConnection.Connect.Success
    The result is 5



    NOTE: If you get a status of NetConnection.Connect.InvalidApp, then try restarting your computer. That is what I had to do. I also noticed that when I restarted my computer, the testapp.war file had disappeared from my webapps directory where Red5 is installed (which is fine because it is not needed...it is only there to deploy the project...once it is deployed it is not needed anymore.)
« Back to Home