1,接着第二篇的文章,书写flash的发送与接收程序
来讲讲设计的思路与原理:
1,flash连接服务器,通过NetConnection 携带用户名信息
username = loginName.text; nc = new NetConnection(); nc.addEventListener( NetStatusEvent.NET_STATUS , netStatus );// connect to red5, passing false as parameter nc.connect( "rtmp://127.0.0.1/red5_5", username);
使用到的As3中NetConnection的方法如下:
(1)connect :用于网络的连接 第一个参数可以写Url地址
第二个参数是:携带的用户信息
(2) addEventListener 第一个参数是传入 netStatus 函数的参数值。
其包含的数据有连接是否成功,如上图是连接成功是,返回“NetConnect.....Success”;
而red5服务端,是这样运行的:
package com.red5app; import java.util.HashMap; import java.util.List; import java.util.Map; import org.red5.server.adapter.ApplicationAdapter; import org.red5.server.api.IConnection; import org.red5.server.api.Red5; import org.red5.server.api.scope.IScope; import org.red5.server.api.service.IServiceCapableConnection; import org.red5.server.api.so.ISharedObject; public class Application extends ApplicationAdapter{ private Map<String,IConnection> clientDic = new HashMap<String,IConnection>(); //最开始执行的程序 public Boolean appStart(){ return true; } //断开连接时执行的程序 public void appStop(){ } //连接时执行的程序 public boolean connect (IConnection conn,IScope scope,Object[] params){ //1,判断是否有用户名
if(params.length==0){ rejectClient("Must pass the login name."); } String loginname=params[0].toString();
conn.setAttribute("UserName", loginname); if(!clientDic.keySet().contains(loginname)) {
clientDic.put(loginname, conn);
//2 ,新建作用域SharedObject ISharedObject so = getSharedObject(scope, "UserSO"); if(so==null) { this.createSharedObject(scope, "UserSO", true); so = getSharedObject(scope, "UserSO"); } so.setAttribute("userlist", clientDic.keySet().toArray()); } else{ rejectClient("The same user has logged in already"); } return super.connect(conn, scope, params); } public void disconnect (IConnection conn,IScope scope){ String loginname=conn.getAttribute("UserName").toString(); if(loginname!=null){ clientDic.remove(loginname); ISharedObject so= getSharedObject(scope,"UserSO"); if(so!=null){ so.setAttribute("userlist",clientDic.keySet().toArray()); } } super.disconnect(conn, scope); }
}
如下图所示:
flash连接成功后,获取连接对象,绑定事件,这里引入一个新的函数SharedObject
SharedObject :(共享对象) 相当于一个数据库,可以在本地或者服务端存放数据。
//新建一个SharedObject
var so:SharedObject;
//获取服务端的SharedObject so = SharedObject.getRemote("UserSO",nc.uri,true);
//添加监听 so.addEventListener(SyncEvent.SYNC, onSync1); //连接 so.connect(nc);
getRemote,包含三个参数,第一个是key值,第二个是地址,第三个是是否持续连接。
SyncEvent:(同布事件)连接成功后,系统会连接信息自动同步相应的数据 SyncEvent.SYNC,绑定以下事件 如下消息
event: [SyncEvent type="sync" bubbles=false cancelable=false eventPhase=2 changeList=[object Object]]
function onSync1(event:SyncEvent):void { trace("event: " + event); for (var i:Object in event.changeList) { var changeObj:Object = event.changeList[i]; trace("Change Code:" + changeObj.code); switch (changeObj.code) { case "success" : break; case "change" : userList.dataProvider = new DataProvider(so.data.userlist); break; } } }
4,最后是发送数据
flah端
function sendwordFunc(e:MouseEvent) { if (nc != null && nc.connected) {if (chatWord.text != "") { nc.call("sendMessage", null, username + "says: " + chatWord.text); chatWord.text = ""; } } }
关键是调用nc.call("sendMessage", null, username + "says: " + chatWord.text);
用过call函数调用服务端中 java程序中的sendMessage函数
如下:
public void sendMessage(List<String> params) { IConnection conn = Red5.getConnectionLocal(); IScope scope = conn.getScope(); ISharedObject so = getSharedObject(scope, "UserSO"); for(int i=0;i<params.size();i++){ log.info("接收到的数据是: " + params.get(i)); } params.add("KBQ"); so.sendMessage("receiveMsg", params); }
如下图所示表示发送数据成功。
之后是服务端回调代码
如下:java代码
public void sendMessage(List<String> params) { IConnection conn = Red5.getConnectionLocal(); IScope scope = conn.getScope(); ISharedObject so = getSharedObject(scope, "UserSO"); for(int i=0;i<params.size();i++){ log.info("接收到的数据是: " + params.get(i)); } params.add("KBQ"); so.sendMessage("receivePrvMsg", params); }
java发送消息给
flash客户端
function receivePrvMsg(sender:String, msg:String):void { // to do … trace(msg); chatArea.appendText(msg + "\n"); }
并显示出来,如下图表示连接成功
代码下载:http://download.csdn.net/download/quankangquan/10110692 可以的话点个好。如果需要合作的话请在下面回复!