您的位置:首页 > 运维架构 > Tomcat

在tomcat下开发Red5应用

2009-11-03 12:36 344 查看
客户端和服务器端的方法相互调用比较重要,在线列表基本上全是用这种方式实现的,当然也有使用RemoteSharedObject来实现的,但本人不太喜欢用RemoteSharedObject,只是用RemoteSharedObject来进行广播等操作。

1.编辑第二篇(抛弃LCDS和FMS,在tomcat下开发Red5应用(第二篇)-建立新的Red5应用)中的Application.java:

Java代码



package red5.example.red5server;

import org.red5.server.adapter.ApplicationAdapter;

import org.red5.server.api.IConnection;

import org.red5.server.api.Red5;

import org.red5.server.api.service.IServiceCapableConnection;

public class Application extends ApplicationAdapter {

private String userName;

//客户端调用的方法

public String callFromClient(String userName) {

this.userName = userName;

callClient();

return "Hello:"+userName;

}

//服务器端调用客户端的方法

public void callClient() {

IConnection conn=Red5.getConnectionLocal();

if (conn instanceof IServiceCapableConnection) {

IServiceCapableConnection sc = (IServiceCapableConnection) conn;

sc.invoke("callFromServer", new Object[]{"hi,"+userName+" this message from server"});

}

}

}

package red5.example.red5server;

import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IServiceCapableConnection;

public class Application extends ApplicationAdapter {

private String userName;

//客户端调用的方法
public String callFromClient(String userName) {
this.userName = userName;
callClient();
return "Hello:"+userName;
}

//服务器端调用客户端的方法
public void callClient() {
IConnection conn=Red5.getConnectionLocal();
if (conn instanceof IServiceCapableConnection) {
IServiceCapableConnection sc = (IServiceCapableConnection) conn;
sc.invoke("callFromServer", new Object[]{"hi,"+userName+" this message from server"});
}
}
}

PS:记得将编译好的class文件放入webapps/ROOT/WEB-INF/classes。

2.编辑第二篇(抛弃LCDS和FMS,在tomcat下开发Red5应用(第二篇)-建立新的Red5应用)中的red5client001.mxml:

Xml代码



<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">

<mx:Script>

<![CDATA[

import flash.net.*;

import flash.events.*;

import flash.utils.*;

import mx.controls.*;

private var nc:NetConnection;

public function init():void {

nc = new NetConnection();

nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);

nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);

nc.connect("rtmp://localhost/red5Server");

nc.client = this;

}

private function netStatus(event:NetStatusEvent):void {

var connStatus:String = event.info.code;

//Alert.show(connStatus);

if(connStatus == "NetConnection.Connect.Success") {

nc.call("callFromClient",new Responder(callServerMethodResult,callServerMethodFault),Math.random().toString());

}

}

private function netSecurityError(event:SecurityErrorEvent):void {

Alert.show("netSecurityError: " + event);

}

public function callServerMethodResult(re:String):void {

Alert.show("客户端调用服务器端方法成功,返回结果:"+re);

}

public function callServerMethodFault(fo:Object):void {

Alert.show("客户端调用服务器端方法失败:"+fo.toString());

}

public function callFromServer(re:Object):void {

Alert.show("服务器端调用客户端方法,传递的参数为:"+re.toString());

}

]]>

</mx:Script>

</mx:Application>

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import flash.net.*;
import flash.events.*;
import flash.utils.*;
import mx.controls.*;

private var nc:NetConnection;

public function init():void {
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);
nc.connect("rtmp://localhost/red5Server");
nc.client = this;
}

private function netStatus(event:NetStatusEvent):void {
var connStatus:String = event.info.code;
//Alert.show(connStatus);
if(connStatus == "NetConnection.Connect.Success") {
nc.call("callFromClient",new Responder(callServerMethodResult,callServerMethodFault),Math.random().toString());
}
}

private function netSecurityError(event:SecurityErrorEvent):void {
Alert.show("netSecurityError: " + event);
}

public function callServerMethodResult(re:String):void {
Alert.show("客户端调用服务器端方法成功,返回结果:"+re);
}

public function callServerMethodFault(fo:Object):void {
Alert.show("客户端调用服务器端方法失败:"+fo.toString());
}

public function callFromServer(re:Object):void {
Alert.show("服务器端调用客户端方法,传递的参数为:"+re.toString());
}
]]>
</mx:Script>
</mx:Application>

将编译好的swf拷贝到webapps/red5Server目录下。

3.重新启动tomcat 运行浏览器,在地址栏输入http://localhost:8080/red5Server/red5client001.html 看到弹出窗口了吧。

客户端和服务器端方法相互调用就这么简单,实际上原有的基于FMS开发的Flex客户端代码修改量是非常小的。在下一篇将讲解客户端和服务器端方法调用中的参数传递。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐