标签:tar gets passing xtend put 网络 code img stop
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); }
}
如下图所示:
3,最后是发送与接收信息:
标签:tar gets passing xtend put 网络 code img stop
原文地址:http://www.cnblogs.com/kbqLibrary/p/7805384.html