忙了好几天,最近也算把Red5服务器服务端基本搞定了。
为了熟悉服务端代码,以及了解API,我仿照着FMS的模式做了一个多人聊天室。基本实现了视频、群聊、私聊几个基本功能。看到网上似乎还没有人放出这类的源代码,我索性就先当回螃蟹吧!
我们先来看代码:
服务端:
Application.java
package org.jerry.videochat;
import java.util.*;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
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;
private ISharedObject listSO;
private ISharedObject msgSO;
private Map<String, IConnection> onLineClient = new HashMap<String, IConnection>();
// 方法
// 此应用开始运行时触发的方法
public boolean appStart(IScope app) {
if (!super.appStart(app)) {
return false;
}
appScope = app;
return true;
}
// 客户端连接的时候触发的方法
public boolean appConnect(IConnection conn, Object[] params) {
username = (String) params[0];
// 登入时将连接ID和连接信息形成对应关系并存入在线列表
String link_id = conn.getClient().getId();
onLineClient.put(username, conn);
// 为用户列表共享对象添加属性
// 创建用户列表共享对象
listSO = getSharedObject(appScope, "listSO", false);
// 创建用户聊天内容共享对象
msgSO = getSharedObject(appScope, "msgSO", false);
listSO.setAttribute(link_id, username);
return true;
}
// 广播消息
public void broadcastUserMsg(String msg) {
// 公聊
// 刷新共享对象属性
msgSO.setAttribute("msg", msg);
}
// 私聊信息
public void msgFromPrivate(String msg, String from, String to) {
IServiceCapableConnection fc = (IServiceCapableConnection) onLineClient
.get(from);
IServiceCapableConnection tc = (IServiceCapableConnection) onLineClient
.get(to);
fc.invoke("showMsgByPrivate", new Object[] { msg });
tc.invoke("showMsgByPrivate", new Object[] { msg });
}
// 用户断开连接的时候触发
public void appDisconnect(IConnection conn) {
String dis_link_id &#
import java.util.*;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
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;
private ISharedObject listSO;
private ISharedObject msgSO;
private Map<String, IConnection> onLineClient = new HashMap<String, IConnection>();
// 方法
// 此应用开始运行时触发的方法
public boolean appStart(IScope app) {
if (!super.appStart(app)) {
return false;
}
appScope = app;
return true;
}
// 客户端连接的时候触发的方法
public boolean appConnect(IConnection conn, Object[] params) {
username = (String) params[0];
// 登入时将连接ID和连接信息形成对应关系并存入在线列表
String link_id = conn.getClient().getId();
onLineClient.put(username, conn);
// 为用户列表共享对象添加属性
// 创建用户列表共享对象
listSO = getSharedObject(appScope, "listSO", false);
// 创建用户聊天内容共享对象
msgSO = getSharedObject(appScope, "msgSO", false);
listSO.setAttribute(link_id, username);
return true;
}
// 广播消息
public void broadcastUserMsg(String msg) {
// 公聊
// 刷新共享对象属性
msgSO.setAttribute("msg", msg);
}
// 私聊信息
public void msgFromPrivate(String msg, String from, String to) {
IServiceCapableConnection fc = (IServiceCapableConnection) onLineClient
.get(from);
IServiceCapableConnection tc = (IServiceCapableConnection) onLineClient
.get(to);
fc.invoke("showMsgByPrivate", new Object[] { msg });
tc.invoke("showMsgByPrivate", new Object[] { msg });
}
// 用户断开连接的时候触发
public void appDisconnect(IConnection conn) {
String dis_link_id &#