package red5.example.red5server; import java.util.Collection; import java.util.HashMap; 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.service.IServiceCapableConnection; import org.red5.server.api.so.ISharedObject; import com.makefriend.entity.UserInfo; import com.makefriend.service.UserInfoService; import com.makefriend.service.impl.UserInfoServiceImpl; 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>(); @Override public boolean appStart(IScope scope) { if (!super.appStart(scope)) { return false; } appScope = scope; return true; } @Override public boolean appConnect(IConnection conn, Object[] params) { System.out.println("red5Server--有客户端要建立连接..."); username = (String) params[0]; UserInfoService userInfoServer = new UserInfoServiceImpl(); UserInfo user = userInfoServer.getUserInfo(username); if (user == null) { return false; } if (!user.getUserPassword().equals(params[1].toString())) { return false; } 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; } /** * 消息群发 * * @param msg */ public void broadcastUserMsg(String msg) { msgSO.setAttribute("msg", msg); Collection<IConnection> cons= onLineClient.values(); for(IConnection i : cons){ IServiceCapableConnection ic=(IServiceCapableConnection) i; ic.invoke("showMsgByPrivate", new Object[] { msg }); } } /** * 私聊信息 * * @param msg * @param from * @param to */ @SuppressWarnings("unchecked") 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 }); } @Override public void appDisconnect(IConnection conn) { System.out.println("red5Server--客户端断开连接..."); String dis_link_id = conn.getClient().getId(); String user = (String) listSO.getAttribute(dis_link_id); onLineClient.remove(user); listSO.removeAttribute(dis_link_id); super.appDisconnect(conn); } } 如果有人关注,我会给出注释版....