在上一篇演示了客户端和服务器端方法调用,其实已经一并演示了参数传递的过程,但都是基本数据类型(ps:AS3的String,int,Number,Boolean,Array,Object对应到red5这边是String,int,double,boolean,java.utils.List,java.utils.Map,参考【red5学习2—参数传递】和 AS3与Red5之间的参数传递
),实际应用中传递的复杂数据类型应该更多,那么我们对上一篇的代码进行略微的修改,从服务器端发送一个自定义的java对象到客户端(参考中的两个例子实际上已经讲述的很清楚了):
1.添加一个自定义类User.java:
- package red5.example.red5server;
- public class User {
- private String userId;
- private String userName;
- private String sex;
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public String getUserId() {
- return userId;
- }
- public void setUserId(String userId) {
- this.userId = userId;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- }
2.修改上一篇的Application.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;
- User user = new User();
- user.setUserId("userId");
- user.setUserName("userName");
- user.setSex("女");
- sc.invoke("callFromServer", new Object[]{user});
- }
- }
- }
3.同样,稍微修改客户端代码:
- <?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("服务器端调用客户端方法,传递的参数为一个对象:\nuserId:"+re.userId+"\nuserName:"+re.userName+"\nsex:"+re.sex);
- }
- ]]>
- </mx:Script>
- </mx:Application>
运行客户端,可以看到从服务器端传递过来的实例已经能被客户端所识别了。
本例子没有像上面两个参考中那样详细,是因为我认为复杂数据类型的传递在RemoteObject中使用更好,而NetConnection上进行服务器端和客户端方法相互调用更适合一些简单的数据传递(可能是原来用FMS的时候基于NetConnection进行参数传递有些难度造成的,呵呵,毕竟FMS端是AS2的语法)。
在下一篇中将介绍如何在Red5中使用RemoteObject,因为很多原来基于LCDS的应用大部分数据操作都是用RemoteObject来实现的,想平滑过渡到Red5上,尽量不要修改Flex端的代码最好。