在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"});
}
}
}
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>
将编译好的swf拷贝到webapps/red5Server目录下。
3.重新启动tomcat 运行浏览器,在地址栏输入http://localhost:8080/red5Server/red5client001.html 看到弹出窗口了吧。
客户端和服务器端方法相互调用就这么简单,实际上原有的基于FMS开发的Flex客户端代码修改量是非常小的。在下一篇将讲解客户端和服务器端方法调用中的参数传递。
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客户端代码修改量是非常小的。在下一篇将讲解客户端和服务器端方法调用中的参数传递。
相关文章推荐
- 抛弃LCDS和FMS,在tomcat下开发Red5应用(第二篇)-建立新的Red5应用
- 抛弃LCDS和FMS,在tomcat下开发Red5应用(第六篇)-如何使用RemoteObject
- 抛弃LCDS和FMS,在tomcat下开发Red5应用(引子)
- 抛弃LCDS和FMS,在tomcat下开发Red5应用(第三篇)-ApplicationAdapte
- 抛弃LCDS和FMS,在tomcat下开发Red5应用(第一篇)-将Red5部署到tomcat中
- 抛弃LCDS和FMS,在tomcat下开发Red5应用(第四篇)-客户端和服务器端的方法互相调用
- 抛弃LCDS和FMS,在tomcat下开发Red5应用(第五篇)-客户端和服务器端方法调用的参数传递
- 基于内嵌Tomcat的应用开发
- Eclipse/TomCat上的J2EE应用开发软件架构4
- 使用jBPM开发企业流程应用之在Tomcat上部署流程引擎及控制台
- Red5集成到Tomcat6.0下并进行新项目的开发配置
- 通过开发工具发布web应用到tomcat服务器中--对于小白,大神可以忽略不看,勿喷,谢谢
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 开源视频会议bigbluebutton开发(5)——开发web端,red5应用,桌面共享
- 各种框架(Struts、Spring、Hibernate)及应用服务器(Tomcat、WebSphere)对应的开发环境(JDK、Servlet、JSP)
- tomcat中的开发应用系统常见的配置[转]
- tomcat中的开发应用系统常见的配置
- 基于Tomcat5.0和Axis2开发Web Service应用实例
- 开源视频会议bigbluebutton开发(5)——开发web端,red5应用,桌面共享
- eclipse开发servlet应用,Tomcat无法访问jpg图片