1. 配置多个webscope
将每一个应用配置到相应的XXX-web.xml 文件中,如下应用red5serv,增加red5serv-web.xml配置文件
内容如下:
<bean id="web.context.red5serv" class="org.red5.server.Context">
<property name="scopeResolver" ref="red5.scopeResolver" />
<property name="clientRegistry" ref="global.clientRegistry" />
<property name="serviceInvoker" ref="global.serviceInvoker" />
<property name="mappingStrategy" ref="global.mappingStrategy" />
</bean>
<bean id="web.scope.red5serv" class="org.red5.server.WebScope" init-method="register">
<property name="server" ref="red5.server" />
<property name="parent" ref="global.scope" />
<property name="context" ref="web.context.red5serv" />
<property name="handler" ref="web.handler.red5serv" />
<property name="contextPath" value="/red5serv" />
<property name="virtualHosts" value="*,localhost, localhost:8888, 127.0.0.1:8888" />
</bean>
<bean id="web.handler.red5serv" class="red5.handler.MainHandler"></bean>
2.利用default scope共享数据
defaultContext.xml 配置文件中的Handler部分改成下面
<bean id="global.handler" class="red5.handler.DefaultHandler"/>
public class DefaultHandler extends CoreHandler {
@Override
public boolean start(IScope arg0) {
if(firststart==0){
arg0.getContext().getGlobalScope().setAttribute("default.scope", "defaultscope");
log.info("init global");
}
firststart++;
log.info(" DefaultHandler start " + arg0.getName() );
arg0.setAttribute("currentscope", arg0.getName() + " ok");
return super.start(arg0);
}
}
每个应用启动前会先调用start方法.
3.在相同的scope 中 shareObject
java application handler
public void updateSO( Object[] value)
{
so.beginUpdate();
so.setAttribute( "test", value );
so.endUpdate();
}
@Override
public boolean roomStart(IScope room) {
log.info("roomStart ");
if (!super.roomStart(room))
return false;
createSharedObject( room, "test", true );
so = getSharedObject( room, "test" );
so.addSharedObjectListener(new SampleSharedObjectListener());
so.registerServiceHandler(new MySharedObjectHandler());
log.info("roomStart test so");
return true;
}
flex
public function onConnection():void{
Logger.info("连接服务器成功,开始测试服务器方法");
so = SharedObject.getRemote( "test", _nc.uri, true );
so.client = this;
so.connect( _nc );
so.addEventListener( SyncEvent.SYNC, alertSO );
}
public function alertSO( e:SyncEvent = null ):void
{
Alert.show( so.data.test.toString() );
}
public function sendSO( ):void
{
_nc.call( "updateSO", null, ti.text );
}
<mx:TextInput id="ti" />
<mx:Button label="send" click="sendSO()" x = "250"/>
<mx:Button label="Aler Shared Object" click="alertSO()" x="500"/>
4.在jsp中直接访问red5服务器中的方法
<%
String url = "http://localhost:8888/red5serv/gateway";
RemotingClient client = new RemotingClient(url);
Object[] args = new Object[]{"Hello world!"};
client.invokeMethod(".getServerM", args, new IRemotingCallback(){
public void errorReceived(RemotingClient client, String method,
Object[] params, Throwable error) {
error.printStackTrace();
}
public void resultReceived(RemotingClient client, String method,
Object[] params, Object result) {
System.out.print(result.toString());
}
});
%>