---
title: "RED5遍历客户端及生成在线列表"
author: "qzriso"
date: "2011-05-05T17:24:10Z"
tags: []
categories: ["Web前端"]
slug: "red5"
draft: false
lastmod: "2011-05-05T17:24:31Z"
id: 1030470
url: "https://strawhat.iteye.com/blog/1030470"
---

在线列表格式为：  
连接ID1,连接用户名1;连接ID2,连接用户名2;...

 

`/* * * D5Power Studio [www.d5power.com] * Code:D5.Benmouse * */ import org.red5.server.adapter.ApplicationAdapter; import org.red5.server.api.IClient; import org.red5.server.api.IConnection; import org.red5.server.api.IScope; import org.red5.server.api.Red5; import java.util.*; public class Application extends ApplicationAdapter { private IScope appScope; private String username=""; //取得本次连接的IScope //appStart将在连接开始的时候自动触发，等同于FMS的onAppStart public boolean appStart(IScope app) { appScope = app; return true; } //连接时触发的函数，定义本过程中的username，等同于FMS的onConnect public boolean appConnect(IConnection conn, Object[] params) { username=(String)params[0]; return true; } //连接加入时触发的函数，写入username的值 public boolean appJoin(IClient client, IScope app) { client.setAttribute("username",username); return true; } //客户端调用函数，将返回目前登陆的在线列表 public String login() { IConnection current = Red5.getConnectionLocal(); System.out.println("<---"+current.getClient().getId()+":"+current.getClient().getAttribute("username")); return getOnlineList(); } //取得在线列表，对在线的客户端进行遍历，并显示。 public String getOnlineList() { Iterator<IConnection> it=appScope.getConnections(); String onLineList=""; while(it.hasNext()) { IConnection this_conn=it.next(); IClient ic=this_conn.getClient(); String u=ic.getAttribute("username").toString(); onLineList+=ic.getId()+","+u+";"; System.out.println(u); } System.out.println("--->"); return onLineList; } public boolean sendMSG() {        //IScope scope = conn.getScope();        Iterator<IConnection> it = appScope.getConnections();        String i="";        while(it.hasNext())        {         IConnection this_conn=it.next();         i+=this_conn.getClient().getAttribute("username")+",";        } return true; } }`