ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Getting started with red5 server
http://www.actionscript.org/resources/articles/615/1/Getting-started-with-red5-server/Page1.html
Milan Toth
Milan Toth is the Chief Flash Developer of Jasmin Media Group, he created one of the world's biggest flash media server system. He loves Eclipse and OS X, AS3 and JAVA, sci-fi and horror, metal and electronic.
 
By Milan Toth
Published on May 29, 2007
 
How to create a simple red5 application.

Part One - Setting up environment
Altough we are on Actionscript.org, this article is mainly about java, ha-ha :D But do not fear my friend, you will need red5 sooner or later for your flash project.
I created this tutorial on OS X, so there can be difference on other oses.

Download and install red5 v0.6 from here.  ( Using this version is recommended, because there can be big differences between releases ). You will need an Eclipse too. Install both with basic parameters. XMLBuddy also comes handy when editing the configuration xml’s, just copy it under Eclipse/plugins. For flash compiling you should use Adobe Flex 2 as an Eclipse plugin, because in this case you can edit and complie everything in Eclipse under one workspace.

If your os doesn’t have a default JRE or JDK, you have to install one, get one from Sun. In this case you have to play with the java CLASSPATH also, check Sun's pages for os specific howtos.

Okay, let's examine the root folder of Red5, red5.jar contains the red5-related packages, webapps folder is the home for our applications. Jetty servlet container will look for applications inside this folder, searching for configuration xml’s under WEB-INF folders.

We can create a red5 application many ways, we can use apache ant to compile our new application based on red5/build.xml, but it's a little bit complicated. We will use Eclipse to write and compile our application, then start red5.

Start Eclipse, File menuitem-> New Project, choose Java Project, press Next, type Red5FirstApp for Project Name, press Finish.

There is a brand new project in Package Explorer, with a standard JRE system library. Right click to the project, New -> Folder, type WEB-INF. Create a classes and a src folder under WEB-INF .

Right click on WEB-INF/src folder -> Build Path -> Use as Source Folder, and you set WEB-INF/src folder as the root folder of our java sources.

We have to set WEB-INF/classes folder as the output folder for our build. Project menuitem -> Properties -> Java Build Path menuitem -> Source Path tab > Default Output Folder at the bottom -> Choose, and choose WEB-INF/classes.

We are almost ready, only the configuration files are missing from a proper Spring injection.

Start your favourite file manager, change to  red5/doc/templates/myapp/WEB-INF folder, and copy every file from there to EclipseWorkspace/Red5FirstApp/WEB-INF.

Switch back to Eclipse, right click on Red5FirstApp project, choose Refresh, and the four files appear under WEB-INF.



Part Two - Writing a basic application

Everything is ready to create a brand new application. Right click on WEB-INF/src, choose New -> Class, type com.milgra for package, Application for name, then press Finish. Our java source appeared in the editor v

This will be our main class, so we need to extend it from the ApplicationAdapter class of red5. Add red5.jar to our build path first. Project menuitem -> Properties -> Java Build Path -> Libraries -> Add External JARs, choose red5.jar in the root folder of red5. Now Eclipse can compile and tip for us.

Create appStart and appStop functions first:

[as]
package com.milgra;
import org.red5.server.adapter.ApplicationAdapter;
public class Application extends ApplicationAdapter
{
    public Boolean appStart ( )
    {

    }
    public void appStop ( )
    {

    }
}
[/as]

Seeing client connections would be good, but we have to import a new class for this:

[as]
import org.red5.server.api.IConnection;
[/as]

so:

[as]
public boolean appConnect( IConnection conn , Object[] params )
{
    return true;
}

public void appDisconnect( IConnection conn , Object[] params )
{

}
[/as]

To debug our application we need logging, so create a logger what prints our logs to the standard output. Lets import a log and logfactory.

[as]
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
[/as]

But something is wrong, because Eclipse marks this two rows with red underline, what is the problem? Eclipse doesn’t know about org.apache.commons.logging, lets add its package quickly:

Project menuitem -> Properties -> Build path -> Libraries -> Add External JARs - pick commons-logging-1.1.jar from folder red5/lib, press OK.

Log a bit:

[as]
private static final Log log = LogFactory.getLog( Application.class );

public boolean appStart ( )
{
    log.info( "Red5First.appStart" );
    return true;
}

public void appStop ( )
{
    log.info( "Red5First.appStop" );
}

public boolean appConnect( IConnection conn , Object[] params )
{
    log.info( "Red5First.appConnect " + conn.getClient().getId() );
    return true;
}

public void appDisconnect( IConnection conn , Object[] params )
{
    log.info( "Red5First.appDisconnect " + conn.getClient().getId() );
}
[/as]

Only a simple demo logic is needed now, lets say if we pass true to the server at connection, we let the client in, if we pass false, we reject it.

[as]
private static final Log log = LogFactory.getLog( Application.class );

public boolean appStart ( )
{
    log.info( "Red5First.appStart" );
    return true;
}

public void appStop ( )
{
    log.info( "Red5First.appStop" );
}

public boolean appConnect( IConnection conn , Object[] params )
{
    log.info( "Red5First.appConnect " + conn.getClient().getId() );

    boolean accept = (Boolean)params[0];

    if ( !accept ) rejectClient( "you passed false..." );

    return true;
}

public void appDisconnect( IConnection conn , Object[] params )
{
    log.info( "Red5First.appDisconnect " + conn.getClient().getId() );
}
[/as]

Final code will look like this:

[as]
package com.milgra;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.red5.server.api.IConnection;
import org.red5.server.adapter.ApplicationAdapter;

public class Application extends ApplicationAdapter
{
    private static final Log log = LogFactory.getLog( Application.class );

    public boolean appStart ( )
    {
        log.info( "Red5First.appStart" );
        return true;
    }

    public void appStop ( )
    {
        log.info( "Red5First.appStop" );
    }

    public boolean appConnect( IConnection conn , Object[] params )
    {
        log.info( "Red5First.appConnect " + conn.getClient().getId() );

        boolean accept = (Boolean)params[0];

        if ( !accept ) rejectClient( "you passed false..." );

        return true;
    }

    public void appDisconnect( IConnection conn , Object[] params )
    {
        log.info( "Red5First.appDisconnect " + conn.getClient().getId() );
    }
   
}
[/as]

We are ready with the java code, only the configuration of the xml’s is needed. Check the four files under WEB-INF folder.

log4j.properties :

application-related logging parameters

red5-web.properties :

this file is included by red5-web.xml, constant parameters can be placed here. webapp.contextPath will be the path to our application at connection, ( not the folder name under webapps!!! ), set it to firstapp

[as]
webapp.contextPath=/firstapp
webapp.virtualHosts=localhost, 127.0.0.1
[/as]

and save it.

red5-web.xml:

spring loads and configures our application based on this file. bean handlers can also be here.

Our web handler will be the Application created above, so set it as:

[as]
    <bean id="web.handler"
        class="com.milgra.Application"
        singleton="true" />
[/as]

And delete myhandler.service, we don’t need it now.

web.xml :

jetty will read this first. Rename the application here to firstapp, and delete the two gateway-related nodes, why use gateway when direct database connection is avaiable from java? :)

[as]
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>/firstapp</param-value>
    </context-param>
[/as]


OK, lets Build the project. Project menuitem -> Build Automatically, so Eclipse will recompile after every modification, and that's great.

Set up our new application under red5: create a new folder under red5/webapps named firstapp ( this is what we set in red5-web.properties ), and copy here WEB-INF from EclipseWorkspace/Red5FirstApp. Then start red5 with your os-specific startup file in the root of red5, and our application is ready.


Part Three - Creating flash client

Okay, server-side is ready, but we should test it somehow.


Create a new Actionscript project in Eclipse/Flex, and name it Red5FirstClient.

[as]
package
{

    import flash.net.NetConnection;
    import flash.net.ObjectEncoding;
    import flash.events.NetStatusEvent;
    import flash.display.Sprite;

    public class Red5FirstClient extends Sprite
    {

        private var nc:NetConnection;

        public function Red5FirstClient()
        {

            // new netconnection

            nc = new NetConnection( );

            // set encoding to old amf

            nc.objectEncoding = ObjectEncoding.AMF0;

            // netstatus event listening

            nc.addEventListener( NetStatusEvent.NET_STATUS , netStatus );

            // connect to red5, passing false as parameter

            nc.connect( "rtmp://localhost/firstapp" , false );

        }

        private function netStatus ( event:NetStatusEvent ):void
        {

            trace( event.info.code );

            if ( event.info.code == "NetConnection.Connect.Rejected" )
            {

                // trace reject message

                trace( event.info.application );

            }

        }

    }

}
[/as]

Run menuitem -> Debug. Because we passed false as connection parameter, our red5 application will reject us, check the last line of red5/jvm log in terminal.

[as]
[INFO] 87028 pool-2-thread-8:( com.milgra.Application.appConnect ) Red5First.appConnect 1
[/as]

so we were connected, let’s check the eclipse/flex console:

[as]
NetConnection.Connect.Rejected
you passed fals . . .
NetConnection.Connect.Closed
[/as]

The server application rejected us, and it passed the rejection message also, it worked as we planned.

Let's change the connection parameter to true, and wonder happens:

[as]
nc.connect( "rtmp://localhost/firstapp" , true );
[/as]

the result is:

[as]
NetConnection.Connect.Success
[/as]

Our application is working!!!