red5 视频应用开发指南。第二个项目成功运行经验(2)
1,第二个项目通过最开始使用书本介绍的red5 1.0.0 ,发现连接的时候总是出现
NetConnection.Connect.Failed undefined
连接出错,具体代码是这样的
服务端代码
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){ 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); 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); } /** * @category 发送全局消息,所有上线的人都能够看到 * @param params */ public void sendMessage(List<String> params) { IConnection conn = Red5.getConnectionLocal(); IScope scope = conn.getScope(); ISharedObject so = getSharedObject(scope, "UserSO"); // log.info("Server: call sendMessage and params " + params.get(0)); so.sendMessage("receiveMsg", params); } /** * @category 发送私聊信息,只有被选中的人能够看到信息 * @param params */ public void sendPrvMessage(List<String> params) { if(params.size() == 3) { String sender = params.get(0); String content = params.get(1); String receiver = params.get(2); if(clientDic.keySet().contains(receiver)) { IConnection conn = clientDic.get(receiver); if(conn instanceof IServiceCapableConnection) { IServiceCapableConnection sc = (IServiceCapableConnection) conn; sc.invoke("receivePrvMsg", new Object[]{sender,content}); } } } } }
Flash cs3的代码是这样的
import flash.net.NetConnection; import flash.events.NetStatusEvent; import flash.events.MouseEvent; import flash.net.SharedObject; var nc:NetConnection=new NetConnection(); nc.connect("rtmp://127.0.0.1/red5_6","asasa"); nc.addEventListener(NetStatusEvent.NET_STATUS,netStatus); nc.client = this; function netStatus(event : NetStatusEvent ):void { switch(event.info.code){ case "NetConnection.Connect.Rejected": trace("连接被拒绝!");break; case "NetConnection.Connect.Success": var so:SharedObject = SharedObject.getRemote("UserSO",nc.uri,true); so.addEventListener(SyncEvent.SYNC,onSync); so.connect(nc); break; } trace(event.info.code); trace(event.info.application); } function onSync(event:SyncEvent):void{ trace("event"+event); for(var i:Object in event.changeList) { var changeobj:Object =event.changeList[i]; trace("Change Code:"+changeobj.code); } } function receiveMessage(mesg:String):void { trace("mesg"+mesg); }
//发现如果注释掉下面三句就能连接成功,所以可以肯定的是软件哪里出了问题
this.createSharedObject(scope, "UserSO", true);
so = getSharedObject(scope, "UserSO");
so.setAttribute("userlist", clientDic.keySet().toArray());
换个
版本的软件,安装过程是这这样的:
安装步骤:
1).解压压缩文件到没有空格和中文的目录中
2).配置环境变量
RED5_HOME=red5-server的解压目录的地址
在PATH=;%RED5_HOME%;
保存OK!
3).运行cmd
4).进入dos命令窗口,敲入red5 回车 OK!
5).显示服务正在启动中…
出现192.168.1.101-5080 表示启动成功!
这个ip我是配置了red5-server 目录 conf 下面 的red5-properties
文件
初次测试可以不用配置,访问时 用localhost访问即可
如果想要配置,配置即可
没有配置之前的访问链接:http://localhost:5080
配置了IP之后的访问链接:http://192.168.1.108:5080
6)下载oflaDemo,下载 好了之后将文件解压到red5-server目录中 webapps 目录中即可
下面给出oflaDemo的下载地址:
链接:http://pan.baidu.com/s/1ntCiJod 密码:au2z
7).在dos中ctrl+c 按 y 关闭服务器 ,再重新输入red5
启动即可!
8).然后访问
来自:http://blog.csdn.net/tgendure/article/details/50166305
发现可以了,显示连接成功
NetConnection.Connect.Success
null
event[SyncEvent type="sync" bubbles=false cancelable=false eventPhase=2 changeList=[object Object]]
Change Code:change