﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-jay-随笔分类-red5</title><link>http://www.blogjava.net/zibeline/category/26500.html</link><description>jay</description><language>zh-cn</language><lastBuildDate>Fri, 12 Oct 2007 06:58:49 GMT</lastBuildDate><pubDate>Fri, 12 Oct 2007 06:58:49 GMT</pubDate><ttl>60</ttl><item><title>Migration guide from FCS/FMS to Red5</title><link>http://www.blogjava.net/zibeline/archive/2007/10/12/152334.html</link><dc:creator>zibeline</dc:creator><author>zibeline</author><pubDate>Fri, 12 Oct 2007 06:09:00 GMT</pubDate><guid>http://www.blogjava.net/zibeline/archive/2007/10/12/152334.html</guid><wfw:comment>http://www.blogjava.net/zibeline/comments/152334.html</wfw:comment><comments>http://www.blogjava.net/zibeline/archive/2007/10/12/152334.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/zibeline/comments/commentRss/152334.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/zibeline/services/trackbacks/152334.html</trackback:ping><description><![CDATA[site: <a href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt" target="_blank">http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt</a><br />
<br />
<table class="docinfo" rules="none" frame="void">
    <tbody valign="top">
        <tr>
            <th class="docinfo-name">Author:</th>
            <td>Joachim Bauch</td>
        </tr>
        <tr>
            <th class="docinfo-name">Contact:</th>
            <td><span class="link-mailto"><a class="first last reference" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#106;&#111;&#106;&#111;&#64;&#115;&#116;&#114;&#117;&#107;&#116;&#117;&#114;&#46;&#100;&#101;">jojo@struktur.de</a></span></td>
        </tr>
        <tr>
            <th class="docinfo-name">Date:</th>
            <td>2006-11-15 00:22:04 +0100 (Mi, 15 Nov 2006)</td>
        </tr>
        <tr>
            <th class="docinfo-name">Revision:</th>
            <td>1540</td>
        </tr>
        <tr class="field">
            <th class="docinfo-name">Id:</th>
            <td class="field-body">MigrationGuide.txt 1540 2006-11-14 23:22:04Z jbauch</td>
        </tr>
    </tbody>
</table>
<div class="contents topic" id="contents">
<p class="topic-title first"><a name="contents">Contents</a></p>
<ul class="simple">
    <li><a class="reference" id="id2" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#preface" name="id2">Preface</a>
    <li><a class="reference" id="id3" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#application-callbacks" name="id3">Application callbacks</a>
    <ul>
        <li><a class="reference" id="id4" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#interface-iscopehandler" name="id4">Interface IScopeHandler</a>
        <li><a class="reference" id="id5" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#class-applicationadapter" name="id5">Class ApplicationAdapter</a>
        <ul>
            <li><a class="reference" id="id6" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#execution-order-of-connection-methods" name="id6">Execution order of connection methods</a> </li>
        </ul>
        <li><a class="reference" id="id7" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#accepting-rejecting-clients" name="id7">Accepting / rejecting clients</a> </li>
    </ul>
    <li><a class="reference" id="id8" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#current-connection-and-client" name="id8">Current connection and client</a>
    <li><a class="reference" id="id9" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#additional-handlers" name="id9">Additional handlers</a>
    <ul>
        <li><a class="reference" id="id10" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#handlers-in-configuration-files" name="id10">Handlers in configuration files</a>
        <li><a class="reference" id="id11" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#handlers-from-application-code" name="id11">Handlers from application code</a> </li>
    </ul>
    <li><a class="reference" id="id12" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#calls-to-client-methods" name="id12">Calls to client methods</a>
    <li><a class="reference" id="id13" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#sharedobjects" name="id13">SharedObjects</a>
    <ul>
        <li><a class="reference" id="id14" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#serverside-change-listeners" name="id14">Serverside change listeners</a>
        <li><a class="reference" id="id15" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#changing-from-application-code" name="id15">Changing from application code</a>
        <li><a class="reference" id="id16" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#sharedobject-event-handlers" name="id16">SharedObject event handlers</a> </li>
    </ul>
    <li><a class="reference" id="id17" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#persistence" name="id17">Persistence</a>
    <li><a class="reference" id="id18" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#periodic-events" name="id18">Periodic events</a>
    <li><a class="reference" id="id19" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#remoting" name="id19">Remoting</a>
    <ul>
        <li><a class="reference" id="id20" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#remoting-server" name="id20">Remoting server</a>
        <li><a class="reference" id="id21" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#remoting-client" name="id21">Remoting client</a> </li>
    </ul>
    <li><a class="reference" id="id22" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#streams" name="id22">Streams</a> </li>
</ul>
</div>
<div class="section" id="preface">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id2" name="preface">Preface</a></h4>
<p>This document describes API differences between the Macromedia Flash Communication Server / Flash Media Server 2 and Red5. It aims at helping migrate existing applications to Red5.</p>
<p>If you don't have an application in Red5 yet, please read the tutorial about <a class="reference" href="http://www.joachim-bauch.de/tutorials/red5/HOWTO-NewApplications.txt">howto create new applications</a> first.</p>
</div>
<div class="section" id="application-callbacks">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id3" name="application-callbacks">Application callbacks</a></h4>
<p>When implementing serverside applications, one of the most important functionalities is to get notified about clients that connect or disconnect and to be informed about the creation of new instances of the application.</p>
<div class="section" id="interface-iscopehandler">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id4" name="interface-iscopehandler">Interface IScopeHandler</a></h5>
<p>Red5 specifies these actions in the interface <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/IScopeHandler.html">IScopeHandler</a></span>. See the API documentation for further details.</p>
</div>
<div class="section" id="class-applicationadapter">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id5" name="class-applicationadapter">Class ApplicationAdapter</a></h5>
<p>As some methods may be called multiple times for one request (e.g. <cite>connect</cite> will be called once for every scope in the tree the client connects to), the class <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/adapter/ApplicationAdapter.html">ApplicationAdapter</a></span> defines additional methods.</p>
<p>This class usually is used as base class for new applications.</p>
<p>Here is a short overview of methods of the FCS / FMS <cite>application</cite> class and their corresponding methods of <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/adapter/ApplicationAdapter.html">ApplicationAdapter</a></span> in Red5:</p>
<table class="docutils" border="1">
    <colgroup>
    <col width="50%">
    <col width="50%"></colgroup>
    <thead valign="bottom">
        <tr>
            <th class="head">FCS / FMS</th>
            <th class="head">Red5</th>
        </tr>
    </thead>
    <tbody valign="top">
        <tr>
            <td>onAppStart</td>
            <td>appStart roomStart</td>
        </tr>
        <tr>
            <td>onAppStop</td>
            <td>appStop roomStop</td>
        </tr>
        <tr>
            <td>onConnect</td>
            <td>appConnect roomConnect appJoin roomJoin</td>
        </tr>
        <tr>
            <td>onDisconnect</td>
            <td>appDisconnect roomDisconnect appLeave roomLeave</td>
        </tr>
    </tbody>
</table>
<p>The <cite>app*</cite> methods are called for the main application, the <cite>room*</cite> methods are called for rooms (i.e. instances) of the application.</p>
<p>You can also also use the <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/adapter/ApplicationAdapter.html">ApplicationAdapter</a></span> to check for streams, shared objects, or subscribe them. See the API documentation for further details.</p>
<div class="section" id="execution-order-of-connection-methods">
<h6><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id6" name="execution-order-of-connection-methods">Execution order of connection methods</a></h6>
<p>Assuming you connect to <cite>rtmp://server/app/room1/room2</cite></p>
<p>At first, the connection is established, so the user "connects" to all scopes that are traversed up to <cite>room2</cite>:</p>
<ol class="arabic simple">
    <li><cite>app</cite> (-&gt; appConnect)
    <li><cite>room1</cite> (-&gt; roomConnect)
    <li><cite>room2</cite> (-&gt; roomConnect) </li>
</ol>
<p>After the connection is established, the client object is retrieved and if it's the first connection by this client to the scope, he "joins" the scopes:</p>
<ol class="arabic simple">
    <li><cite>app</cite> (-&gt; appJoin)
    <li><cite>room1</cite> (-&gt; roomJoin)
    <li><cite>room2</cite> (-&gt; roomJoin) </li>
</ol>
<p>If the same client establishes a second connection to the same scope, only the <cite>connect</cite> methods will be called. If you conect to partially the same scopes, only a few <cite>join</cite> methods might be called, e.g. <cite>rtmp://server/app/room1/room3</cite> will trigger</p>
<ol class="arabic simple">
    <li><cite>appConnect("app")</cite>
    <li><cite>joinConnect("room1")</cite>
    <li><cite>joinConnect("room3")</cite>
    <li><cite>roomJoin("room3")</cite> </li>
</ol>
<p>The <cite>appStart</cite> method currently is only called once during startup of Red5 as it currently can't unload/load applications like FCS/FMS does. The <cite>roomStart</cite> methods are called when the first client connects to a room.</p>
</div>
</div>
<div class="section" id="accepting-rejecting-clients">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id7" name="accepting-rejecting-clients">Accepting / rejecting clients</a></h5>
<p>FCS / FMS provide the methods <cite>acceptConnection</cite> and <cite>rejectConnection</cite> to accept and reject new clients. To allow clients to connect, no special action is required by Red5 applications, the <cite>*Connect</cite> methods just need to return <cite>true</cite> in this case.</p>
<p>If a client should not be allowed to connect, the method <cite>rejectClient</cite> can be called which is implemented by the <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/adapter/ApplicationAdapter.html">ApplicationAdapter</a></span> class. Any parameter passed to <cite>rejectClient</cite> is available as the <cite>application</cite> property of the status object that is returned to the caller.</p>
</div>
</div>
<div class="section" id="current-connection-and-client">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id8" name="current-connection-and-client">Current connection and client</a></h4>
<p>Red5 supports two different ways to access the current connection from an invoked method. The connection can be used to get the active client and the scope he is connected to. The first possibility uses the "magic" <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/Red5.html">Red5</a></span> object:</p>
<pre class="literal-block">import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
public void whoami() {
IConnection conn = Red5.getConnectionLocal();
IClient client = conn.getClient();
IScope scope = conn.getScope();
// ...
}
</pre>
<p>The second possiblity requires the method to be defined with an argument of type <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/IConnection.html">IConnection</a></span> as implicit first parameter which is automatically added by Red5 when a client calls the method:</p>
<pre class="literal-block">import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
public void whoami(IConnection conn) {
IClient client = conn.getClient();
IScope scope = conn.getScope();
// ...
}
</pre>
</div>
<div class="section" id="additional-handlers">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id9" name="additional-handlers">Additional handlers</a></h4>
<p>For many applications, existing classes containing application logic that is not related to Red5 are required to be reused. In order to make them available for clients connecting through RTMP, these classes need to be registered as handlers in Red5.</p>
<dl class="docutils">
<dt>There are currently two ways to register these handlers:
<dd>
<ol class="first last arabic simple">
    <li>By adding them to the configuration files.
    <li>By registering them manually from the application code. </li>
</ol>
</dd></dl>
<p>The handlers can be executed by clients with code similar to this:</p>
<pre class="literal-block">nc = new NetConnection();
nc.connect("rtmp://localhost/myapp");
nc.call("handler.method", nc, "Hello world!");
</pre>
<p>If a handler is requested, Red5 always looks it up in the custom scope handlers before checking the handlers that have been set up in the context through the configuration file.</p>
<div class="section" id="handlers-in-configuration-files">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id10" name="handlers-in-configuration-files">Handlers in configuration files</a></h5>
<p>This method is best suited for handlers that are common to all scopes the application runs in and that don't need to change during the lifetime of an application.</p>
<p>To register the class <cite>com.fancycode.red5.HandlerSample</cite> as handler <cite>sample</cite>, the following bean needs to be added to <cite>WEB-INF/red5-web.xml</cite>:</p>
<pre class="literal-block">&lt;bean id="sample.service"
class="com.fancycode.red5.HandlerSample"
singleton="true" /&gt;
</pre>
<p>Note that the id of the bean is constructed as the name of the handler (here <cite>sample</cite>) and the keyword <cite>service</cite>.</p>
</div>
<div class="section" id="handlers-from-application-code">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id11" name="handlers-from-application-code">Handlers from application code</a></h5>
<p>All applications that use handlers which are different for the various scopes or want to change handlers, need a way to register them from the serverside code. These handlers always override the handlers configured in <cite>red5-web.xml</cite>. The methods required for registration are described in the interface <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/service/IServiceHandlerProvider.html">IServiceHandlerProvider</a></span> which is implemented by <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/adapter/ApplicationAdapter.html">ApplicationAdapter</a></span>.</p>
<p>The same class as above can be registered using this code:</p>
<pre class="literal-block">public boolean appStart(IScope app) {
if (!super.appStart(scope))
return false;
Object handler = new com.fancycode.red5.HandlerSample();
app.registerServiceHandler("sample", handler);
return true;
}
</pre>
<p>Note that in this example, only the application scope has the <cite>sample</cite> handler but not the subscopes! If the handler should be available in the rooms as well, it must be registered in <cite>roomStart</cite> for the room scopes.</p>
</div>
</div>
<div class="section" id="calls-to-client-methods">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id12" name="calls-to-client-methods">Calls to client methods</a></h4>
<p>To call methods from your Red5 application on the client, you will first need a reference to the current connection object:</p>
<pre class="literal-block">import org.red5.server.api.IConnection;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IServiceCapableConnection;
...
IConnection conn = Red5.getConnectionLocal();
</pre>
<p>If the connection implements the <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/service/IServiceCapableConnection.html">IServiceCapableConnection</a></span> interface, it supports calling methods on the other end:</p>
<pre class="literal-block">if (conn instanceof IServiceCapableConnection) {
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("the_method", new Object[]{"One", 1});
}
</pre>
<p>If you need the result of the method call, you must provide a class that implements the <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/service/IPendingServiceCallback.html">IPendingServiceCallback</a></span> interface:</p>
<pre class="literal-block">import org.red5.server.api.service.IPendingService;
import org.red5.server.api.service.IPendingServiceCallback;
class MyCallback implements IPendingServiceCallback {
public void resultReceived(IPendingServiceCall call) {
// Do something with "call.getResult()"
}
}
</pre>
<p>The method call looks now like this:</p>
<pre class="literal-block">if (conn instanceof IServiceCapableConnection) {
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("the_method", new Object[]{"One", 1}, new MyCallback());
}
</pre>
<p>Of course you can implement this interface in your application and pass a reference to the application instance.</p>
</div>
<div class="section" id="sharedobjects">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id13" name="sharedobjects">SharedObjects</a></h4>
<p>The methods to access shared objects from an application are specified in the interface <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/so/ISharedObjectService.html">ISharedObjectService</a></span>.</p>
<p>When dealing with shared objects in serverside scripts, special care must be taken about the scope they are created in.</p>
<p>To create a new shared object when a room is created, you can override the method <cite>roomStart</cite> in your application:</p>
<pre class="literal-block">import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IScope;
import org.red5.server.api.so.ISharedObject;
public class SampleApplication extends ApplicationAdapter {
public boolean roomStart(IScope room) {
if (!super.roomStart(room))
return false;
createSharedObject(room, "sampleSO", true);
ISharedObject so = getSharedObject(room, "sampleSO");
// Now you could do something with the shared object...
return true;
}
}
</pre>
<p>Now everytime a first user connects to a room of a application, e.g. through <cite>rtmp://server/application/room1</cite>, a shared object <cite>sampleSO</cite> is created by the server.</p>
<p>If a shared object should be created for connections to the main application, e.g. <cite>rtmp://server/application</cite>, the same must be done in the method <cite>appStart</cite>.</p>
<p>For further informations about the possible methods a shared object provides please refer to the api documentation of the interface <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/so/ISharedObject.html">ISharedObject</a></span>.</p>
<div class="section" id="serverside-change-listeners">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id14" name="serverside-change-listeners">Serverside change listeners</a></h5>
<p>To get notified about changes of the shared object similar to <cite>onSync</cite> in FCS / FMS, a listener must implement the interface <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/so/ISharedObjectListener.html">ISharedObjectListener</a></span>:</p>
<pre class="literal-block">import org.red5.server.api.so.ISharedObject;
import org.red5.server.api.so.ISharedObjectListener;
public class SampleSharedObjectListener
implements ISharedObjectListener {
public void onSharedObjectUpdate(ISharedObject so,
String key, Object value) {
// The attribute &lt;key&gt; of the shared object &lt;so&gt;
// was changed to &lt;value&gt;.
}
public void onSharedObjectDelete(ISharedObject so, String key) {
// The attribute &lt;key&gt; of the shared object &lt;so&gt; was deleted.
}
public void onSharedObjectSend(ISharedObject so,
String method, List params) {
// The handler &lt;method&gt; of the shared object &lt;so&gt; was called
// with the parameters &lt;params&gt;.
}
// Other methods as described in the interface...
}
</pre>
<p>Additionally, the listener must get registered at the shared object:</p>
<pre class="literal-block">ISharedObject so = getSharedObject(scope, "sampleSO");
so.addSharedObjectListener(new SampleSharedObjectListener())
</pre>
</div>
<div class="section" id="changing-from-application-code">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id15" name="changing-from-application-code">Changing from application code</a></h5>
<p>A shared object can be changed by the server as well:</p>
<pre class="literal-block">ISharedObject so = getSharedObject(scope, "sampleSO");
so.setAttribute("fullname", "Sample user");
</pre>
<p>Here all subscribed clients as well as the registered handlers are notified about the new / changed attribute.</p>
<p>If multiple actions on a shared object should be combined in one update event to the subscribed clients, the methods <cite>beginUpdate</cite> and <cite>endUpdate</cite> must be used:</p>
<pre class="literal-block">ISharedObject so = getSharedObject(scope, "sampleSO");
so.beginUpdate();
so.setAttribute("One", "1");
so.setAttribute("Two", "2");
so.removeAttribute("Three");
so.endUpdate();
</pre>
<p>The serverside listeners will receive their update notifications through separate method calls as without the <cite>beginUpdate</cite> and <cite>endUpdate</cite>.</p>
</div>
<div class="section" id="sharedobject-event-handlers">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id16" name="sharedobject-event-handlers">SharedObject event handlers</a></h5>
<p>Calls to shared object handlers through <cite>remote_so.send(&lt;handler&gt;, &lt;args&gt;)</cite> from a Flash client or the corresponding serverside call can be mapped to methods in Red5. Therefore a handler must get registered through a method of the <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/so/ISharedObjectHandlerProvider.html">ISharedObjectHandlerProvider</a></span> interface similar to the application handlers:</p>
<pre class="literal-block">package com.fancycode.red5;
class MySharedObjectHandler {
public void myMethod(String arg1) {
// Now do something
}
}
...
ISharedObject so = getSharedObject(scope, "sampleSO");
so.registerServiceHandler(new MySharedObjectHandler());
</pre>
<p>Handlers with a given name can be registered as well:</p>
<pre class="literal-block">ISharedObject so = getSharedObject(scope, "sampleSO");
so.registerServiceHandler("one.two", new MySharedObjectHandler());
</pre>
<p>Here, the method could be called through <cite>one.two.myMethod</cite>.</p>
<p>Another way to define event handlers for SharedObjects is to add them to the <cite>red5-web.xml</cite> similar to the file-based application handlers. The beans must have a name of <cite>&lt;SharedObjectName&gt;.&lt;DottedServiceName&gt;.soservice</cite>, so the above example could also be defined with:</p>
<pre class="literal-block">&lt;bean id="sampleSO.one.two.soservice"
class="com.fancycode.red5.MySharedObjectHandler"
singleton="true" /&gt;
</pre>
</div>
</div>
<div class="section" id="persistence">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id17" name="persistence">Persistence</a></h4>
<p>Persistence is used so properties of objects can be used even after the server has been restarted. In FCS / FMS usually local shared objects on the serverside are used for this.</p>
<p>Red5 allows arbitrary objects to be persistent, all they need to do is implement the interface <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/persistence/IPersistable.html">IPersistable</a></span>. Basically these objects have a <cite>type</cite>, a <cite>path</cite>, a <cite>name</cite> (all strings) and know how to serialize and deserialize themselves.</p>
<p>Here is a sample of serialization and deserialization:</p>
<pre class="literal-block">import java.io.IOException;
import org.red5.io.object.Input;
import org.red5.io.object.Output;
import org.red5.server.api.persistence.IPersistable;
class MyPersistentObject implements IPersistable {
// Attribute that will be made persistent
private String data = "My persistent value";
void serialize(Output output) throws IOException {
// Save the objects's data.
output.writeString(data);
}
void deserialize(Input input) throws IOException {
// Load the object's data.
data = input.readString();
}
// Other methods as described in the interface...
}
</pre>
<p>To save or load this object, the following code can be used:</p>
<pre class="literal-block">import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.persistence.IPersistenceStore;
class MyApplication extends ApplicationAdapter {
private void saveObject(MyPersistentObject object) {
// Get current scope.
IScope scope = Red5.getConnectionLocal().getScope();
// Save object in current scope.
scope.getStore().save(object);
}
private void loadObject(MyPersistentObject object) {
// Get current scope.
IScope scope = Red5.getConnectionLocal().getScope();
// Load object from current scope.
scope.getStore().load(object);
}
}
</pre>
<p>If no custom objects are required for an application, but data must be stored for future reuse, it can be added to the <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/IScope.html">IScope</a></span> through the interface <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/IAttributeStore.html">IAttributeStore</a></span>. In scopes, all attributes that don't start with <cite>IPersistable.TRANSIENT_PREFIX</cite> are persistent.</p>
<p>The backend that is used to store objects is configurable. By default persistence in memory and in the filesystem is available.</p>
<p>When using filesystem persistence for every object a file is created in "webapps/&lt;app&gt;/persistence/&lt;type&gt;/&lt;path&gt;/&lt;name&gt;.red5", e.g. for a shared object "theSO" in the connection to "rtmp://server/myApp/room1" a file at "webapps/myApp/persistence/SharedObject/room1/theSO.red5" would be created.</p>
</div>
<div class="section" id="periodic-events">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id18" name="periodic-events">Periodic events</a></h4>
<p>Applications that need to perform tasks regularly can use the <cite>setInterval</cite> in FCS / FMS to schedule methods for periodic execution.</p>
<p>Red5 provides a scheduling service (<span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/scheduling/ISchedulingService.html">ISchedulingService</a></span>) that is implemented by <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/adapter/ApplicationAdapter.html">ApplicationAdapter</a></span> like most other services. The service can register an object (which needs to implement the <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/api/scheduling/IScheduledJob.html">IScheduledJob</a></span> interface) whose <cite>execute</cite> method is called in a given interval.</p>
<p>To register an object, code like this can be used:</p>
<pre class="literal-block">import org.red5.server.api.IScope;
import org.red5.server.api.IScheduledJob;
import org.red5.server.api.ISchedulingService;
import org.red5.server.adapter.ApplicationAdapter;
class MyJob implements IScheduledJob {
public void execute(ISchedulingService service) {
// Do something
}
}
public class SampleApplication extends ApplicationAdapter {
public boolean roomStart(IScope room) {
if (!super.roomStart(room))
return false;
// Schedule invokation of job every 10 seconds.
String id = addScheduledJob(10000, new MyJob());
room.setAttribute("MyJobId", id);
return true;
}
}
</pre>
<p>The id that is returned by <cite>addScheduledJob</cite> can be used later to stop execution of the registered job:</p>
<pre class="literal-block">public void roomStop(IScope room) {
String id = (String) room.getAttribute("MyJobId");
removeScheduledJob(id);
super.roomStop(room);
}
</pre>
</div>
<div class="section" id="remoting">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id19" name="remoting">Remoting</a></h4>
<p>Remoting can be used by non-rtmp clients to invoke methods in Red5. Another possibility is to call methods from Red5 to other servers that provide a remoting service.</p>
<div class="section" id="remoting-server">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id20" name="remoting-server">Remoting server</a></h5>
<p>Services that should be available for clients need to be registered the same way as additional application handlers are registered. See above for details.</p>
<p>To enable remoting support for an application, the following section must be added to the <cite>WEB-INF/web.xml</cite> file:</p>
<pre class="literal-block">&lt;servlet&gt;
&lt;servlet-name&gt;gateway&lt;/servlet-name&gt;
&lt;servlet-class&gt;
org.red5.server.net.servlet.AMFGatewayServlet
&lt;/servlet-class&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;gateway&lt;/servlet-name&gt;
&lt;url-pattern&gt;/gateway/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</pre>
<p>The path specified in the <cite>&lt;url-pattern&gt;</cite> tag (here <cite>gateway</cite>) can be used by the remoting client as connection url. If this example would have been specified for an application <cite>myApp</cite>, the URL would be:</p>
<pre class="literal-block">http://localhost:5080/myApp/gateway
</pre>
<p>Methods invoked through this connection will be executed in the context of the application scope. If the methods should be executed in subscopes, the path to the subscopes must be added to the URL like:</p>
<pre class="literal-block">http://localhost:5080/myApp/gateway/room1/room2
</pre>
</div>
<div class="section" id="remoting-client">
<h5><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id21" name="remoting-client">Remoting client</a></h5>
<p>The class <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/net/remoting/RemotingClient.html">RemotingClient</a></span> defines all methods that are required to call methods through the remoting protocol.</p>
<p>The following code serves as example about how to use the remoting client:</p>
<pre class="literal-block">import org.red5.server.net.remoting.RemotingClient;
String url = "http://server/path/to/service";
RemotingClient client = new RemotingClient(url);
Object[] args = new Object[]{"Hello world!"};
Object result = client.invokeMethod("service.remotingMethod", args);
// Now do something with the result
</pre>
<p>By default, a timeout of 30 seconds will be used per call, this can be changed by passing a second parameter to the constructor defining the maximum timeout in milliseconds.</p>
<p>The remoting headers <cite>AppendToGatewayUrl</cite>, <cite>ReplaceGatewayUrl</cite> and <cite>RequestPersistentHeader</cite> are handled automatically by the Red5 remoting client.</p>
<p>Some methods may take a rather long time on the called server to complete, so it's better to perform the call asynchronously to avoid blocking a thread in Red5. Therefore an object that implements the interface <span class="link-external"><a class="reference" href="http://dl.fancycode.com/red5/api/org/red5/server/net/remoting/IRemotingCallback.html">IRemotingCallback</a></span> must be passed as additional parameter:</p>
<pre class="literal-block">import org.red5.server.net.remoting.RemotingClient;
import org.red5.server.net.remoting.IRemotingCallback;
public class CallbackHandler implements IRemotingCallback {
void errorReceived(RemotingClient client, String method,
Object[] params, Throwable error) {
// An error occurred while performing the remoting call.
}
void resultReceived(RemotingClient client, String method,
Object[] params, Object result) {
// The result was received from the server.
}
}
String url = "http://server/path/to/service";
RemotingClient client = new RemotingClient(url);
Object[] args = new Object[]{"Hello world!"};
IRemotingCallback callback = new CallbackHandler();
client.invokeMethod("service.remotingMethod", args, callback);
</pre>
</div>
</div>
<div class="section" id="streams">
<h4><a class="toc-backref" href="http://www.joachim-bauch.de/tutorials/red5/MigrationGuide.txt#id22" name="streams">Streams</a></h4>
</div>
<img src ="http://www.blogjava.net/zibeline/aggbug/152334.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/zibeline/" target="_blank">zibeline</a> 2007-10-12 14:09 <a href="http://www.blogjava.net/zibeline/archive/2007/10/12/152334.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>