获取某一房间的用户
This commit is contained in:
@@ -7,7 +7,7 @@ import Offer from './offer';
|
||||
import Answer from './answer';
|
||||
import Candidate from './candidate';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { onGetAllConnectionIds } from './websockethandler';
|
||||
import { onGetAllConnectionIds, onGetOnlineUsers as onGetWsOnlineUsers } from './websockethandler';
|
||||
import { log, LogLevel } from '../log';
|
||||
/**
|
||||
* 断开连接记录类
|
||||
@@ -1106,6 +1106,66 @@ function getAllConnectionIds(req: Request, res: Response): void {
|
||||
// 返回JSON响应,包含连接ID列表和总数量
|
||||
res.json({ connectionIds: connectionIds, totalCount: connectionIds.length });
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线WebSocket用户列表
|
||||
* @param req HTTP请求对象
|
||||
* @param res HTTP响应对象
|
||||
*/
|
||||
/**
|
||||
* @swagger
|
||||
* /signaling/users:
|
||||
* get:
|
||||
* summary: 获取在线WebSocket用户列表
|
||||
* description: 获取所有在线WebSocket用户,支持按 connectionId 过滤指定房间内的用户
|
||||
* parameters:
|
||||
* - in: query
|
||||
* name: connectionId
|
||||
* schema:
|
||||
* type: string
|
||||
* required: false
|
||||
* description: 连接ID,传入时仅返回该房间内的在线用户
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 成功获取在线用户列表
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* users:
|
||||
* type: array
|
||||
* items:
|
||||
* type: object
|
||||
* properties:
|
||||
* connectionId:
|
||||
* type: string
|
||||
* description: 所属连接ID
|
||||
* participantId:
|
||||
* type: string
|
||||
* description: 参与者ID
|
||||
* role:
|
||||
* type: string
|
||||
* enum: [host, participant]
|
||||
* description: 角色
|
||||
* userId:
|
||||
* type: string
|
||||
* description: 用户ID
|
||||
* name:
|
||||
* type: string
|
||||
* description: 用户名称
|
||||
* avatar:
|
||||
* type: string
|
||||
* description: 用户头像URL
|
||||
* totalCount:
|
||||
* type: number
|
||||
* description: 在线用户总数
|
||||
*/
|
||||
function getOnlineUsers(req: Request, res: Response): void {
|
||||
const connectionId = typeof req.query.connectionId === 'string' ? req.query.connectionId : undefined;
|
||||
const users = onGetWsOnlineUsers(connectionId);
|
||||
res.json({ users: users, totalCount: users.length });
|
||||
}
|
||||
/**
|
||||
* 导出HTTP处理器函数
|
||||
*/
|
||||
@@ -1125,5 +1185,6 @@ export {
|
||||
postAnswer, // 处理answer信令消息
|
||||
postCandidate, // 处理candidate信令消息
|
||||
onGetConnections, // 获取房间和用户信息
|
||||
getAllConnectionIds // 获取所有连接ID
|
||||
getAllConnectionIds, // 获取所有连接ID
|
||||
getOnlineUsers // 获取在线WebSocket用户列表
|
||||
};
|
||||
|
||||
@@ -29,6 +29,21 @@ interface ConnectionGroup {
|
||||
participants: Set<WebSocket>;
|
||||
}
|
||||
|
||||
interface UserInfo {
|
||||
id: string;
|
||||
name: string;
|
||||
avatar?: string;
|
||||
}
|
||||
|
||||
interface OnlineUser {
|
||||
connectionId: string;
|
||||
participantId: string;
|
||||
role: 'host' | 'participant';
|
||||
userId: string;
|
||||
name: string;
|
||||
avatar: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接组映射
|
||||
* 键: connectionId
|
||||
@@ -440,6 +455,53 @@ function onGetAllConnectionIds(): string[] {
|
||||
return connectionIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将WebSocket连接转换为在线用户信息
|
||||
* @param ws WebSocket连接实例
|
||||
* @param connectionId 连接ID
|
||||
* @param role 用户角色
|
||||
* @returns 在线用户信息
|
||||
*/
|
||||
function toOnlineUser(ws: WebSocket, connectionId: string, role: 'host' | 'participant'): OnlineUser {
|
||||
const userInfo = ((ws as any).userInfo || {}) as UserInfo;
|
||||
return {
|
||||
connectionId: connectionId,
|
||||
participantId: (ws as any).participantId || '',
|
||||
role: role,
|
||||
userId: userInfo.id || '',
|
||||
name: userInfo.name || '',
|
||||
avatar: userInfo.avatar || ''
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线WebSocket用户列表
|
||||
* @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'));
|
||||
});
|
||||
});
|
||||
return onlineUsers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理chat-message信令(1对多模式)
|
||||
* host的消息转发给所有participants,participant的消息转发给host
|
||||
@@ -451,6 +513,13 @@ function onMessage(ws: WebSocket, message: any): void {
|
||||
const connectionId = message.connectionId;
|
||||
const chatMessage = message.message;
|
||||
const senderParticipantId = (ws as any).participantId;
|
||||
if (chatMessage && chatMessage.type === 'user-info' && chatMessage.data) {
|
||||
(ws as any).userInfo = {
|
||||
id: chatMessage.data.id || '',
|
||||
name: chatMessage.data.name || '匿名用户',
|
||||
avatar: chatMessage.data.avatar || ''
|
||||
};
|
||||
}
|
||||
chatMessage.participantId = senderParticipantId;
|
||||
chatMessage.connectionId = connectionId;
|
||||
if (connectionGroup.has(connectionId)) {
|
||||
@@ -476,4 +545,4 @@ function onMessage(ws: WebSocket, message: any): void {
|
||||
/**
|
||||
* 导出WebSocket处理器函数
|
||||
*/
|
||||
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, onBroadcast, onGetAllConnectionIds, AddHeartbeat, RemoveHeartbeat, onMessage, isHost, broadcastToGroup, connectionGroup };
|
||||
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, onBroadcast, onGetAllConnectionIds, onGetOnlineUsers, AddHeartbeat, RemoveHeartbeat, onMessage, isHost, broadcastToGroup, connectionGroup };
|
||||
|
||||
@@ -5,6 +5,7 @@ const router: express.Router = express.Router();
|
||||
|
||||
// 不需要会话ID的路由
|
||||
router.get('/connection-ids', handler.getAllConnectionIds);
|
||||
router.get('/users', handler.getOnlineUsers);
|
||||
|
||||
// 需要会话ID的路由
|
||||
router.use(handler.checkSessionId);
|
||||
|
||||
Reference in New Issue
Block a user