Flex4 实现视频通话完整实例 二 服务器端

Flex4 实现视频通话完整实例 二 服务器端

一、GlobalConstance.java

package com.hsj.red5.server;

public class GlobalConstance {

 /**
  * 更新客户端用户列表的方法
  */
 public static final String RESULT_GETONLOADERUSER="result_getOnLoadUser";

 /**
  * 显示客户端消息的方法
  */
 public static final String SHOWMESSAGE="showMessage";
 
 /**
  * 视频被邀请方调用的方法
  */
 public static final String SHOWINVITEMESSAGE="showInviteMessage";
 
 /**
  * 邀请方接到被邀请方同意后调用的方法
  */
 public static final String SHOWVIDEO="showVideo";
 
 /**
  * 对方拒绝视频后调用的客户端方法
  */
 public static final String MSGTIP="msgTip";
 
 /**
  * 用户断开连接后调用的方法
  */
 public static final String DISCONNECTMESSAGE="disConnectMessage";
}

 

二、Application.java

package com.hsj.red5.server;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.service.IServiceCapableConnection;
import org.red5.server.api.so.ISharedObject;

public class Application extends ApplicationAdapter {

 /**
  * 范围对象
  */
 private IScope appScope;
 
 /**
  * 用户名
  */
 private String userName;
 
 /**
  * 存储所有共享对象<用户Id,用户名>
  */
 private ISharedObject listSO;
 
 /**
  * 存储所有的在线用户列表
  */
 private Map<String,IConnection> onLineList=new HashMap<String,IConnection>();
 
 @Override
 public boolean appConnect(IConnection conn, Object[] args) {
  // TODO Auto-generated method stub
  System.out.println("服务器连接成功!");
  String userId=conn.getClient().getId();
  
  
  if(args!=null){
   this.userName=args[0].toString();
  }
  
  if(this.onLineList.get(this.userName)!=null){
   rejectClient("请不要重复登录!");
   return false;
  }
  
  this.onLineList.put(this.userName,conn);
  
  //通过名字在指定返回获得共享对象
  this.listSO=this.getSharedObject(appScope,"listSO",false);
  //设置共享对象的属性
  this.listSO.setAttribute(userId,this.userName);
  
  System.out.println("客户端编号为"+userId+"的用户姓名为:"+this.userName);
  
  return true;
 }

 @Override
 public boolean appStart(IScope app) {
  // TODO Auto-generated method stub
  if(!super.appStart(app)){
   return false;
  }
  
  this.appScope=app;
  return  true;
 }
 
 public void getOnLoadUser(Object[] params){
  if(params==null || params.length==0 ||
    params[0]==null || "".equals(params[0])){
   return ;
  }
  
  //向所有登录的客户端发送消息
  this.callEveryOne(GlobalConstance.RESULT_GETONLOADERUSER,params);
 }

 private void callEveryOne(String methodName, Object[] params) {
  // TODO Auto-generated method stub
  
  IScope scope=Red5.getConnectionLocal().getScope();
  
  Iterator<IConnection> iters=scope.getConnections();
  
  while(iters.hasNext()){
   IConnection conn=iters.next();
   if(conn instanceof IServiceCapableConnection){
    IServiceCapableConnection sc=(IServiceCapableConnection)conn;
    sc.invoke(methodName,params);
   }
  }
  
 }
 
 /**
  * 聊天的方法
  * @param params
  *  params[0]聊天时的对方姓名 <br/>
  *  params[1]聊天时说话的内容
  */
 public void chat(Object[] params){
  
  IConnection conn=Red5.getConnectionLocal();
  
  String userId=conn.getClient().getId();
  String userName=this.listSO.getAttribute(userId).toString();
  
  System.out.println("发言者:"+userName);
  
  String sayToName="";
  String sayWhat=null;
  if(params!=null && params.length>1){
   sayToName=params[0]==null?"":params[0].toString();
   sayWhat=params[1]==null?"":params[1].toString();
  }
  
  System.out.println(userName+"对"+sayToName+"说"+sayWhat);
  
  if("".equals(sayToName) || "All".equals(sayToName)){
   this.callEveryOne(GlobalConstance.SHOWMESSAGE,new Object[]{userName+"对大家说"+sayWhat});
  }else{
   IConnection otherConn=this.onLineList.get(sayToName);
   
   if(otherConn instanceof IServiceCapableConnection){
    IServiceCapableConnection sc=(IServiceCapableConnection)otherConn;
    sc.invoke(GlobalConstance.SHOWMESSAGE,new Object[]{userName+"对"+sayToName+"说"+sayWhat});
   }
   
   if(conn instanceof IServiceCapableConnection){
    IServiceCapableConnection sc=(IServiceCapableConnection)conn;
    sc.invoke(GlobalConstance.SHOWMESSAGE,new Object[]{userName+"对"+sayToName+"说"+sayWhat});
   }
  }
  
 }
 
 /**
  * 视频邀请方调用的方法
  * @param params params[0] 表示对方的名字
  * @throws Exception
  */
 public void videoInvite(Object[] params) throws Exception{
  IConnection conn=Red5.getConnectionLocal();
  String userId=conn.getClient().getId();
  userName=this.listSO.getAttribute(userId).toString();
  
  System.out.println("邀请者:"+userName);
  
  String otherName=params[0]==null?"":params[0].toString();
  
  System.out.println("otherName:"+otherName);
  if("".equals(otherName) || "All".equals(otherName)){
   System.out.println("不能同时邀请0个或者多个人视频");
   throw new Exception("不能同时邀请0个或者多个人视频");
  }else{
   IConnection otherConn=this.onLineList.get(otherName);
   if(otherConn instanceof IServiceCapableConnection){
    IServiceCapableConnection sc=(IServiceCapableConnection)otherConn;
    sc.invoke(GlobalConstance.SHOWINVITEMESSAGE,new Object[]{this.userName+";"+otherName});
   }
  }
 }
 
 public void agreeVideoInvite(Object[] params) throws Exception{
  if(params!=null && params.length>1){
   String inviteUserName=params[0]==null?"":params[0].toString();
   String otherUserName=params[1]==null?"":params[1].toString();
   System.out.println("视频邀请者:"+inviteUserName);
   System.out.println("视频被邀请者:"+otherUserName);
   
   if("".equals(inviteUserName)){
    System.out.println("邀请人不能为空!");
   }else{
    System.out.println("开始视频:");
    
    IConnection conn=this.onLineList.get(inviteUserName);
    if(conn instanceof IServiceCapableConnection){
     IServiceCapableConnection sc=(IServiceCapableConnection)conn;
     sc.invoke(GlobalConstance.SHOWVIDEO,new Object[]{otherUserName});
    }
    System.out.println("视频结束!");
   }
  }else{
   System.out.println("参数错误!");
   throw new Exception("参数错误!");
  }
 }

 /**
  * 拒绝对方视频时调用的方法
  * @param params params[0]自己(邀请方);params[1] 对方(被邀请方)
  */
 public void denyVideoInvite(Object[] params) throws Exception{
  if(params!=null && params.length>1){
   IConnection conn=this.onLineList.get(params[0]);
   if(conn instanceof IServiceCapableConnection){
    IServiceCapableConnection sc=(IServiceCapableConnection)conn;
    sc.invoke(GlobalConstance.MSGTIP,new Object[]{params[1]+"拒绝了您的请求!"});
   }
  }else{
   System.out.println("参数错误!");
   throw new Exception("参数错误!");
  }
 }

 /**
  * 用户断开连接时调用
  */
 @Override
 public void appDisconnect(IConnection conn) {
  String userId=conn.getClient().getId();
  String userName=this.listSO.getAttribute(userId).toString();
  this.onLineList.remove(userName);
  this.listSO.removeAttribute(userId);
  
  this.callEveryOne(GlobalConstance.DISCONNECTMESSAGE, new Object[]{userName});
  
 }
 
 
}

 

##########################################################

## firefox下正常,IE下运行还有些小bug只能显示对方视频。。。。

##知识很浅,不要见笑,以资鼓励,聊以共勉!

##########################################################

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值