获取全部用户
This commit is contained in:
@@ -1116,8 +1116,8 @@ function getAllConnectionIds(req: Request, res: Response): void {
|
||||
* @swagger
|
||||
* /signaling/users:
|
||||
* get:
|
||||
* summary: 获取在线WebSocket用户列表
|
||||
* description: 获取所有在线WebSocket用户,支持按 connectionId 过滤指定房间内的用户
|
||||
* summary: 获取全部在线WebSocket用户列表
|
||||
* description: 获取所有当前已建立WebSocket连接的用户,包括未加入房间的大厅用户;支持按 connectionId 过滤指定房间内的用户
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: connectionId
|
||||
@@ -1146,8 +1146,11 @@ function getAllConnectionIds(req: Request, res: Response): void {
|
||||
* description: 参与者ID
|
||||
* role:
|
||||
* type: string
|
||||
* enum: [host, participant]
|
||||
* enum: [host, participant, idle]
|
||||
* description: 角色
|
||||
* socketId:
|
||||
* type: string
|
||||
* description: WebSocket连接ID
|
||||
* userId:
|
||||
* type: string
|
||||
* description: 用户ID
|
||||
@@ -1159,7 +1162,7 @@ function getAllConnectionIds(req: Request, res: Response): void {
|
||||
* description: 用户头像URL
|
||||
* totalCount:
|
||||
* type: number
|
||||
* description: 在线用户总数
|
||||
* description: 在线WebSocket用户总数
|
||||
*/
|
||||
function getOnlineUsers(req: Request, res: Response): void {
|
||||
const connectionId = typeof req.query.connectionId === 'string' ? req.query.connectionId : undefined;
|
||||
|
||||
@@ -36,9 +36,10 @@ interface UserInfo {
|
||||
}
|
||||
|
||||
interface OnlineUser {
|
||||
socketId: string;
|
||||
connectionId: string;
|
||||
participantId: string;
|
||||
role: 'host' | 'participant';
|
||||
role: 'host' | 'participant' | 'idle';
|
||||
userId: string;
|
||||
name: string;
|
||||
avatar: string;
|
||||
@@ -88,6 +89,7 @@ function add(ws: WebSocket): void {
|
||||
// 为新连接创建空的连接ID集合
|
||||
const id = new Set<string>();
|
||||
clients.set(ws, id);
|
||||
(ws as any).socketId = (ws as any).socketId || `ws_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
||||
// 记录添加WebSocket连接的日志
|
||||
log(LogLevel.log, `Add WebSocket: ${ws.url}`);
|
||||
}
|
||||
@@ -455,19 +457,38 @@ function onGetAllConnectionIds(): string[] {
|
||||
return connectionIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取WebSocket连接当前角色
|
||||
* @param ws WebSocket连接实例
|
||||
* @param connectionIds 该连接关联的connectionId集合
|
||||
* @returns 用户角色
|
||||
*/
|
||||
function getSocketRole(ws: WebSocket, connectionIds: string[]): 'host' | 'participant' | 'idle' {
|
||||
for (const connectionId of connectionIds) {
|
||||
if (isHost(ws, connectionId)) {
|
||||
return 'host';
|
||||
}
|
||||
const group = connectionGroup.get(connectionId);
|
||||
if (group && group.participants.has(ws)) {
|
||||
return 'participant';
|
||||
}
|
||||
}
|
||||
return 'idle';
|
||||
}
|
||||
|
||||
/**
|
||||
* 将WebSocket连接转换为在线用户信息
|
||||
* @param ws WebSocket连接实例
|
||||
* @param connectionId 连接ID
|
||||
* @param role 用户角色
|
||||
* @returns 在线用户信息
|
||||
*/
|
||||
function toOnlineUser(ws: WebSocket, connectionId: string, role: 'host' | 'participant'): OnlineUser {
|
||||
function toOnlineUser(ws: WebSocket): OnlineUser {
|
||||
const connectionIds = Array.from(clients.get(ws) || []);
|
||||
const userInfo = ((ws as any).userInfo || {}) as UserInfo;
|
||||
return {
|
||||
connectionId: connectionId,
|
||||
socketId: (ws as any).socketId || '',
|
||||
connectionId: connectionIds[0] || '',
|
||||
participantId: (ws as any).participantId || '',
|
||||
role: role,
|
||||
role: getSocketRole(ws, connectionIds),
|
||||
userId: userInfo.id || '',
|
||||
name: userInfo.name || '',
|
||||
avatar: userInfo.avatar || ''
|
||||
@@ -476,28 +497,17 @@ function toOnlineUser(ws: WebSocket, connectionId: string, role: 'host' | 'parti
|
||||
|
||||
/**
|
||||
* 获取在线WebSocket用户列表
|
||||
* @param connectionId 可选的连接ID,传入时仅返回指定房间的在线用户
|
||||
* @param connectionId 可选的连接ID,传入时仅返回关联该房间的在线用户
|
||||
* @returns 在线用户列表
|
||||
*/
|
||||
function onGetOnlineUsers(connectionId?: string): OnlineUser[] {
|
||||
if (connectionId) {
|
||||
const group = connectionGroup.get(connectionId);
|
||||
if (!group) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
toOnlineUser(group.host, connectionId, 'host'),
|
||||
...Array.from(group.participants).map((participantWs) => toOnlineUser(participantWs, connectionId, 'participant'))
|
||||
];
|
||||
}
|
||||
|
||||
const onlineUsers: OnlineUser[] = [];
|
||||
connectionGroup.forEach((group, currentConnectionId) => {
|
||||
onlineUsers.push(toOnlineUser(group.host, currentConnectionId, 'host'));
|
||||
group.participants.forEach((participantWs) => {
|
||||
onlineUsers.push(toOnlineUser(participantWs, currentConnectionId, 'participant'));
|
||||
});
|
||||
clients.forEach((connectionIds, ws) => {
|
||||
const ids = Array.from(connectionIds);
|
||||
if (connectionId && !ids.includes(connectionId)) {
|
||||
return;
|
||||
}
|
||||
onlineUsers.push(toOnlineUser(ws));
|
||||
});
|
||||
return onlineUsers;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user