Intro.
As the very first entry of my very first blog
. Let me describe about what I’m gonna talking about. The Red5, is an open-source data service for flex technology. Many that has been working around the flex should have known this software for quite sometime. But for those who don’t; Flex data service, or from my understanding, it’s a server side of the software. It’s can provided many various kind of service. The Red5 act upon the basis of connection providing between client-server and from that point it’s also provided the client-client.
Now, that’s too much for the brief you can certainly find out more information from their Red5 official site. So, finally, here is the main idea of this entry: “How to use this Red5?”.
Here are some basic example of our well known “Hello, World”.
Let’s start with server side first.
- download the red5 from their svn link address.
- open your eclipse, import the project.
- create a new folder in webapps/[your project’s name]/WEB-INF/src. And link that as a source folder.
- in your project folder, with sub directory “WEB-INF” make sure it contains these files (these files can be copies from the template directory located in doc/templates/myapp/WEB-INF).
- red5-web.properties
- red5-web.xml
- web.xml
- leave these 3 files as it be first. We’ll come back to it later.
- Now let’s start coding!.
Server Side: Java.
From my pov; I rather prefer jdk1.6 instead of 1.5.
To create a new application for Red5 server, that allows an access from client side. You can easily extends the ApplicationAdapter class of Red5.
1: public class Application extends ApplicationAdapter {
2:
3: private static final Log log = LogFactory.getLog( Application.class );
4:
5: @Override
6: public boolean appStart(IScope scope)
7: {
8: // initialize application.
9: log.info("Application.appStart()");
10: return true;
11: }
12:
13: @Override
14: public boolean appConnect( IConnection conn , Object[] params )
15: {
16: log.info("Application.appConnect()");
17: return true;
18: }
19:
20: @Override
21: public void appDisconnect( IConnection conn )
22: {
23: log.info( "Application.appDisconnect()");
24: super.appDisconnect( conn );
25: }
26:
27: @Override
28: public void appStop( IScope app )
29: {
30: log.info( "Application.appStop()");
31: super.appStop( app );
32: }
33: }
this is a very basic code. Which do exactly nothing, just allow the client connect to the server. Now let’s add some method for the client to call from their side.
1: public String hello(String words) {
2: return "Hello " + words;
3: }
the code above is a function that allow the calls from the client which contains one argument, and reply back with the “Hello “ concats to it’s head, and this will be called from client.
That’s much for the server’s code for now.
Server Side: Configure xml files.
Now to enable the Red5 to knows your application, we need to configures the xml file that we left earlier.
- red5-web.properties
- red5-web.xml
- in this file it’s defined the class path that reference to the certain class that will be executed on the client’s requests. Locate the bean with id of “web.handler” and supply your class path to your Application class.
- web.xml
- this file state the lookup path for your application. Configure the parameter by name webAppRootKey will be suffice.
1: webapp.contextPath=/[your project folder name]
2: webapp.virtualHosts=localhost,127.0.0.1
1: <!-- Customized software spring injection -->
2: <bean id="web.handler"
3: class="class.path.to.your.Application"/>
1: <context-param>
2: <param-name>webAppRootKey</param-name>
3: <param-value>/[your project's name]</param-value>
4: </context-param>
that’s much for the configuration you will touch these files again only when you need to add services in your application.
Client Side: Flex.
For flex, create a new actionscript class, To calls the server side. you only need a NetConnection class to made the call to your server. Here is the code sample.
1: package com.sylli.mce.flex.client
2: {
3: import flash.events.NetStatusEvent;
4: import flash.net.NetConnection;
5: import flash.net.ObjectEncoding;
6: import flash.net.Responder;
7:
8: public class Red5Facade
9: {
10: private var m_nc:NetConnection;
11: private var m_rs:Responder;
12:
13: /* constructor */
14: public function Red5Facade( )
15: {
16: trace("Establish connection. ");
17:
18: // create responder for string return-type.
19: m_rs = new Responder(debugStringResponderFunction, null);
20:
21: m_nc = new NetConnection( ); // new netconnection
22: m_nc.objectEncoding = ObjectEncoding.AMF0;
23:
24: // netstatus event listening
25: m_nc.addEventListener( NetStatusEvent.NET_STATUS , netStatus );
26:
27: // connect to red5, passing false as parameter
28: m_nc.connect( "rtmp://localhost/[your project's name]" );
29: }
30:
31: private function netStatus ( event:NetStatusEvent ):void
32: {
33: trace( "NetStatus: " + event.info.code );
34: if ( event.info.code == "NetConnection.Connect.Rejected" )
35: {
36: // trace reject message
37: trace( event.info.application );
38: }
39: }
40:
41: public function helloWorld ( words:String ):void
42: {
43: m_nc.call("hello", m_rs, words);
44: }
45:
46: protected function debugStringResponderFunction(_val:String):void
47: {
48: trace("String returns " + _val);
49: }
50: }
51: }
Finally, the test drive, with your eclipse start the Red5 server by create new run, using org.red5.server.Standalone which will starts all the server nodes existing in their webapps directory.
Then start debug your flex application, try call your class methods. if it made a proper connection to the red5’s. (start as a debug mode, so the trace() function would print properly.
Hope this will help. Any comments will be appreciated.