`
womendu
  • 浏览: 1517063 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

red5-remote-sharedobject-experience-tips

阅读更多

The following post is a copy of a post i originally contributed to the red5 mailing list.

Hello Red5 Community,

i am kind of new to red5 and spend the last days tyring to figure out how to use shared objects with red5.
In the following lines i like to summarise my experiences. This should serve as a hint for others who come accross the same questions and problems that i did.
Please feel free to correct or supplement the following lines where neccessary.
When discussing SharedObjects in the following, i refer to Remote Shared Objects and not Local Shared Objects of course. I also focus on non-persistent ones.

What are SharedObjects good for?

SharedObjects (SOs) are helpful to synchronize data between multiple clients in real-time and to invoke methods on multiple clients at once. A Remote SharedObject is an Object which is shared between multiple clients, namely all clients of the same scope.

So what is a Scope?
You can think of a scope as a chat room which is defined by the uri that is used to connect the server:

rtmp://localhost/red5test/lobby

red5test is the application scope
lobby is a room scope, a child scope of red5test
for more information on scopes see: http://jira.red5.org/confluence/display/docs/Scopes+and+Contexts

How to connect to SOs (clientside)?

A Client can connect to a SO using the following Actionscript lines:

// at first you need to establish a connection to the server of course
nc = new NetConnection();
nc.connect( "rtmp://localhost/red5test/", true );

// when the NetConnection is successfully established
// (you have to listen to NetStatusEvent.NET_STATUS event)
// you can connect to a remote SharedObject e.g. named "chat"
so = SharedObject.getRemote("chat", nc.uri, false);

// when the connection to the SO is successfully established,
// you can begin to specify its contents
so.setProperty("message","hello to all");
How does red5 handle SOs?

If a client connects to a SO, red5 looks into the specified scope if a SO named e.g. “chat” already exists.
If it does, red5 uses it, if not, red5 creates a new SO for the Scope and uses that.
When the last client connected to a SO disconnects from it, the SO will be destroyed (at least for non-persistent ones).

SOs on the serverside

a) Access SOs on the serverside
SOs can also be accessed from the serverside, i.e. the server also can make changes to the SO and broadcast them to all connected clients.

To check wether a certain SO exists you can use the following lines in your Java Application:

//get scope of the current connection first
IScope scope = Red5.getConnectionLocal().getScope();
// get SO
ISharedObject so = getSharedObject(scope, "chat");

b) Create SOs on the serverside
You can also create SOs on the serverside with the following lines:
extracted from Joachim Bauchs tutorial:

To create a new shared object when a room is created, you can override the method roomStart in your application:

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;
  }

}

If a shared object should be created for connections to the main application, e.g. rtmp://server/application, the same must be done in the method appStart.

So what is it good for to create SOs by yourself when red5 does it for you automatically?

One advantage is, that you can attach a Listener to the SO which is notified when a new client connects to the SO, updates data, disconnects etc. (See ISharedObjectListener)

Problem: clientside initiated vs. serverside initiated SOs

Creating SOs in your java application code can leed to some unexpected problems (It was at least for me).
The problem is related to something i refer to as clientside vs. serverside initiated SOs.
Of course all SOs wil be created on the red5 server. But there is a difference who initiated the creation.

  • a) a serverside initiated SO is a SO that you have created with createSharedObject in your serverside code. It is then waiting for clients to connect.
    When a client connects to this SO, your custom implementation will be used.
  • b) a clientside initiated SO is a “generic” SO that red5 automatically creates right in the moment when red5 recognizes that the SO a client connects does not exist.

The problem arises when you want to create a custom serverside SO for the main application:
e.g. I want to create an SO named “chat” in the main application scope and add a listener to it. So i put the following lines in my appStart method:

createSharedObject(scope, "chat", false);
ISharedObject so = getSharedObject(scope, "chat");
ISharedObjectListener listener = new MyCustomListener();
so.addSharedObjectListener(listener);

Now when i start the server, appStart is called once the Application starts. My custom SO named “chat ” will be created (a serverside-initiated SO). Now when a client connects to the SO named “chat” in the main application scope, my custom SO will be used by red5. Everything works fine untill now.
But when all clients disconnect form this SO, my custom, serverside initiated SO will be destroyed by red5. After that, when a client connects to the SO named “chat”, red5 won’t find it anymore and will automatically create a new “genric” one. This new one now is clientside initiated, as i did not create it in the serverside code with createSharedObject. The difference now is, that no listener is attached to the new generic SO, and so no events will be fired anymore.

This problem is not that severe for SOs in a room scope. In this case you put your creation code inside the roomStart method. As the room is also destroyed when the last connected client disconnects, the roomStart method will be called multiple times during the application lifecycle, i.e. everytime the first client connects to a room. In this case the problem only arises when the last client disconnects from the SO but not the room.

So what solution is there to this problem?

It would be good to have something like a SharedObjectCreationHandler or a SharedObjectFactory, but unfortunately there is nothing like that. But there is a Handler which handles sort of SO creation events: the ISharedObjectSecurity.
This handler can be registered in the Application by registerSharedObjectSecurity. Its use is to controll access to shared objects. It contains an event method named isCreationAllowed: Prior to automatically creating a SO red5 checks if creation is allowed by calling this method.
Now if you want a custom serverside initiated SO to be created everytime red5 automatically tries to create a SO, you can put your SO creation code inside this method and return true. Red5 then knows creation is allowed and tries to create a SO. To do this red5f first checks if the SO already exists and !big surprise! it does, so red5 uses it and does not create one on its own.

import static org.red5.server.api.ScopeUtils.getScopeService;
...

  public class MySecurityHandler implements ISharedObjectSecurity
  {

  ...

  public boolean isCreationAllowed(IScope scope, String name, boolean persistent)
  {
    if ( "chat".equals(name) )
    {
      // get the SO creation service. Basically the following lines are exactly
      // what MultiThreadedApplicationAdapter does. You can also pass a reference
      // to your Application to this SecurityHandler and use it instead
      ISharedObjectService service = (ISharedObjectService) getScopeService(
            scope,
            ISharedObjectService.class,
            SharedObjectService.class,
            false
      );

      if( service.createSharedObject(scope, name, persistent) == true )
      {
        ISharedObject so = service.getSharedObject(scope, name);
        ISharedObjectListener listener = new MyCustomListener();
        so.addSharedObjectListener(listener);
      }
    }
    return true;
  }

  ...

}

The method described here, was also discussed in: [Red5] creation handler to ShareObject

Last but not the least: When is it better to use a SharedObject and when to use invokes?

Here is good mail discussing that topic: http://osflash.org/pipermail/red5_osflash.org/2007-August/014406.html

That’s all,
hopefully it helps somebody.

Cheers,
Nik

—————–

Further tutorials on SOs:

分享到:
评论

相关推荐

    red5-1.0.0-webapps-demos.zip

    标题中的"red5-1.0.0-webapps-demos.zip"表明这是一个关于Red5服务器的Web应用程序示例的压缩包。Red5是一款开源的流媒体服务器,它支持实时流传输协议(RTMP)、文件录制、播放以及直播等功能。1.0.0是其版本号,...

    setup-Red5-1.0.0.exe

    setup-Red5-1.0.0.exe 官方安装包 java开源流媒体软件 windows版本

    node-red-contrib-remote:Node-RED的远程访问和推送通知

    它由几个部分组成: node-red-contrib-remote:Remote-RED的Node-RED节点。 在此版本中发布。 Remote-RED服务器:多个服务器,用于代理远程访问流量和发送推送通知。 Remote-RED应用程序:用于访问Remote-RED远程...

    red5-0.9.1.tar.gz

    Red5是一款开源的流媒体服务器,它允许开发者创建实时的、交互式的富互联网应用程序(Rich Internet Applications,简称RIA)。这个“red5-0.9.1.tar.gz”文件是Red5的一个版本,版本号为0.9.1,被打包成.tar.gz格式...

    red-server-1.0.10-M9.tar.gz

    在标题“red-server-1.0.10-M9.tar.gz”中,我们可以看出这是一个Red5服务器的版本号为1.0.10-M9的源码压缩包,文件格式为tar.gz,这是Linux系统中常见的归档压缩格式。这个资源特别适合那些需要在Linux环境下搭建流...

    red5-server.zip

    Red5 Server是一款开源的流媒体服务器,主要用于处理和分发实时数据流,如视频、音频和其他类型的数据。这个"red5-server.zip"压缩包提供了一个免安装版本,使得用户可以在本地快速搭建起一个流媒体服务器,无需复杂...

    red5-service-1.0.2-RELEASE.jar

    编译github上的red5-server发现咩有这个包, 自己编译了一下, 供大家参考.

    setup-Red5-1.0.0-RC1.rar

    Red5是一款开源的流媒体服务器,它支持RTMP(Real Time Messaging Protocol)协议,使得用户可以进行实时音视频传输,广泛应用于在线直播、视频会议、游戏等场景。"setup-Red5-1.0.0-RC1.rar" 是一个包含Red5服务器...

    setup-Red5-1.0.8-M11.exe

    Red5服务器是一款开源的流媒体服务器,主要用于处理和分发实时数据流,如视频、音频以及数据应用。标题中的"setup-Red5-1.0.8-M11.exe"是Red5服务器的安装程序,版本号为1.0.8的M11维护版本。这个安装包适用于...

    red5-server-1.0.5-RELEASE-source.tar.gz

    【red5-server-1.0.5-RELEASE-source.tar.gz】Linux版本的Red5源码 此版本包含几个修复和修改后的 1.0.4 发现释放。Tomcat 插件日志记录使用新的 jar,其日志事件推向 slf4j;更新的也是 websocket 插件到 1.1 版。 ...

    Red5-1.0.0

    setup-Red5-1.0.0-java6.exe 用来搭建 red5 服务器环境;

    red5-server-1.0.10-M4.tar.gz

    Red5 Server是一款开源的、基于Java技术的实时媒体服务器,主要功能是支持视频流和音频流的发布、播放以及录制。版本1.0.10-M4是Red5的一个中期版本,提供了稳定的服务和一系列改进。这个压缩包“red5-server-1.0.10...

    red5-1.0.1 流媒体服务

    Red5是一款开源的Java流媒体服务器,用于处理视频、音频流和实时数据。标题中的"red5-1.0.1 流媒体服务"指的是Red5服务器的一个特定版本,即1.0.1,它专注于提供流媒体内容,如视频和MP3播放。描述中的“视频 MP3 ...

    setup-Red5-0.9.0

    setup-Red5-0.9.0

    red5-eclipse-plugin-master.zip

    Eclipse是Java开发的集成开发环境(IDE),而"red5-eclipse-plugin-master.zip"则是一个专门为Eclipse设计的插件,用于方便开发者在Eclipse中开发、调试和部署Red5应用程序。 该插件提供了以下功能: 1. **项目创建...

    red5-1.0.0下载

    - **red5-highperf.bat、red5.bat、red5-shutdown.bat、red5-debug.bat**:这些都是启动和管理Red5服务器的批处理脚本,分别对应高性能模式、常规模式、关闭服务和调试模式。 - **.classpath、.project**:这是...

    red5-server

    Red5 Server是一款开源的流媒体服务器,主要用于处理和分发实时多媒体数据,如视频和音频流。这个"red5-server"项目包含的就是Red5的源代码,对于开发者来说,它是理解流媒体服务工作原理和自定义功能的一个宝贵资源...

    red5-server-1.2.3.zip

    Red5 Server是一款开源的流媒体服务器软件,专为实时数据通信设计,支持多种协议,如RTMP、HLS、RTSP等。这个压缩包“red5-server-1.2.3.zip”包含了Red5 Server的1.2.3版本,它是从官方网站下载的,因此保证了其...

    red5-server-1.0.6.zip 流媒体服务器

    Red5 Server是一款开源的流媒体服务器,主要用于处理和分发实时的音频、视频内容,如直播、点播等。这个“red5-server-1.0.6.zip”压缩包包含了我亲自编译并测试过的版本,它有大约50MB大小,其中不仅包含了Red5 ...

    red5-1.0.1

    setup-Red5-1.0.1-java6.exe 可用于下载 来播放rtmp

Global site tag (gtag.js) - Google Analytics