获取某一房间的用户
This commit is contained in:
@@ -10,6 +10,7 @@ const MAX_AVATAR_SIZE = 2 * 1024 * 1024; // 2MB
|
|||||||
|
|
||||||
// WebSocket连接状态更新回调
|
// WebSocket连接状态更新回调
|
||||||
let onWsStatusChange = null;
|
let onWsStatusChange = null;
|
||||||
|
let cachedOnlineUsers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置WebSocket状态变化回调
|
* 设置WebSocket状态变化回调
|
||||||
@@ -63,15 +64,27 @@ export async function initWebSocket() {
|
|||||||
async function getAllConnectionIds() {
|
async function getAllConnectionIds() {
|
||||||
showNotification('正在获取连接ID列表...');
|
showNotification('正在获取连接ID列表...');
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/signaling/connection-ids');
|
const [connectionResponse, usersResponse] = await Promise.all([
|
||||||
if (!response.ok) {
|
fetch('/signaling/connection-ids'),
|
||||||
|
fetch('/signaling/users')
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!connectionResponse.ok) {
|
||||||
throw new Error('Failed to fetch connection IDs');
|
throw new Error('Failed to fetch connection IDs');
|
||||||
}
|
}
|
||||||
const data = await response.json();
|
|
||||||
displayConnectionIds(data.connectionIds);
|
if (!usersResponse.ok) {
|
||||||
|
throw new Error('Failed to fetch online users');
|
||||||
|
}
|
||||||
|
|
||||||
|
const connectionData = await connectionResponse.json();
|
||||||
|
const usersData = await usersResponse.json();
|
||||||
|
cachedOnlineUsers = Array.isArray(usersData.users) ? usersData.users : [];
|
||||||
|
displayConnectionIds(connectionData.connectionIds || []);
|
||||||
|
displayOnlineUsers(cachedOnlineUsers);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching connection IDs:', error);
|
console.error('Error fetching connection IDs:', error);
|
||||||
showNotification('获取连接ID失败', 'error');
|
showNotification('获取连接信息失败', 'error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,6 +121,100 @@ function displayConnectionIds(connectionIds) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转义HTML特殊字符
|
||||||
|
* @param {string} value - 原始字符串
|
||||||
|
* @returns {string} 安全字符串
|
||||||
|
*/
|
||||||
|
function escapeHtml(value) {
|
||||||
|
return String(value || '')
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按连接ID分组显示在线用户
|
||||||
|
* @param {Array} users - 在线用户列表
|
||||||
|
* @param {string} selectedConnectionId - 当前选中的连接ID
|
||||||
|
*/
|
||||||
|
function displayOnlineUsers(users, selectedConnectionId = '') {
|
||||||
|
const onlineUsersList = document.getElementById('onlineUsersList');
|
||||||
|
const usersContainer = document.getElementById('usersContainer');
|
||||||
|
const onlineUsersSummary = document.getElementById('onlineUsersSummary');
|
||||||
|
|
||||||
|
if (!onlineUsersList || !usersContainer || !onlineUsersSummary) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filteredUsers = selectedConnectionId
|
||||||
|
? users.filter(user => user.connectionId === selectedConnectionId)
|
||||||
|
: users;
|
||||||
|
|
||||||
|
onlineUsersSummary.textContent = selectedConnectionId
|
||||||
|
? `${filteredUsers.length} 人在线 · 房间 ${selectedConnectionId}`
|
||||||
|
: `${filteredUsers.length} 人在线`;
|
||||||
|
|
||||||
|
usersContainer.innerHTML = '';
|
||||||
|
|
||||||
|
if (filteredUsers.length === 0) {
|
||||||
|
usersContainer.innerHTML = '<p class="text-gray-500 text-sm">暂无在线用户</p>';
|
||||||
|
onlineUsersList.classList.remove('hidden');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const groupedUsers = filteredUsers.reduce((groups, user) => {
|
||||||
|
const connectionId = user.connectionId || 'unknown';
|
||||||
|
if (!groups[connectionId]) {
|
||||||
|
groups[connectionId] = [];
|
||||||
|
}
|
||||||
|
groups[connectionId].push(user);
|
||||||
|
return groups;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
Object.entries(groupedUsers).forEach(([connectionId, roomUsers]) => {
|
||||||
|
const section = document.createElement('div');
|
||||||
|
section.className = 'rounded-lg border border-white/10 bg-white/5 p-3';
|
||||||
|
|
||||||
|
const roomTitle = document.createElement('div');
|
||||||
|
roomTitle.className = 'flex items-center justify-between mb-2';
|
||||||
|
roomTitle.innerHTML = `
|
||||||
|
<span class="text-sm font-medium text-white">${escapeHtml(connectionId)}</span>
|
||||||
|
<span class="text-xs text-gray-400">${roomUsers.length} 人</span>
|
||||||
|
`;
|
||||||
|
section.appendChild(roomTitle);
|
||||||
|
|
||||||
|
const roomList = document.createElement('div');
|
||||||
|
roomList.className = 'space-y-2';
|
||||||
|
|
||||||
|
roomUsers.forEach((user) => {
|
||||||
|
const userName = user.name || user.userId || '匿名用户';
|
||||||
|
const avatar = user.avatar || '/images/p2.png';
|
||||||
|
const roleLabel = user.role === 'host' ? '房主' : '成员';
|
||||||
|
const userItem = document.createElement('div');
|
||||||
|
userItem.className = 'flex items-center justify-between rounded-lg bg-black/20 px-3 py-2';
|
||||||
|
userItem.innerHTML = `
|
||||||
|
<div class="flex items-center gap-3 min-w-0">
|
||||||
|
<img src="${escapeHtml(avatar)}" alt="${escapeHtml(userName)}" class="w-8 h-8 rounded-full object-cover">
|
||||||
|
<div class="min-w-0">
|
||||||
|
<div class="text-sm text-white truncate">${escapeHtml(userName)}</div>
|
||||||
|
<div class="text-xs text-gray-400 truncate">${escapeHtml(user.userId || user.participantId || '未设置ID')}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<span class="text-xs px-2 py-1 rounded-full ${user.role === 'host' ? 'bg-indigo-500/20 text-indigo-300' : 'bg-white/10 text-gray-300'}">${roleLabel}</span>
|
||||||
|
`;
|
||||||
|
roomList.appendChild(userItem);
|
||||||
|
});
|
||||||
|
|
||||||
|
section.appendChild(roomList);
|
||||||
|
usersContainer.appendChild(section);
|
||||||
|
});
|
||||||
|
|
||||||
|
onlineUsersList.classList.remove('hidden');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 选择连接ID
|
* 选择连接ID
|
||||||
* @param {string} id - 连接ID
|
* @param {string} id - 连接ID
|
||||||
@@ -116,6 +223,7 @@ function selectConnectionId(id) {
|
|||||||
const connectionIdInput = document.getElementById('connectionIdInput');
|
const connectionIdInput = document.getElementById('connectionIdInput');
|
||||||
if (connectionIdInput) {
|
if (connectionIdInput) {
|
||||||
connectionIdInput.value = id;
|
connectionIdInput.value = id;
|
||||||
|
displayOnlineUsers(cachedOnlineUsers, id);
|
||||||
showNotification(`已选择连接ID: ${id}`);
|
showNotification(`已选择连接ID: ${id}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -356,4 +464,4 @@ window.selectConnectionId = selectConnectionId;
|
|||||||
window.saveSettings = saveSettings;
|
window.saveSettings = saveSettings;
|
||||||
window.handleAvatarUpload = handleAvatarUpload;
|
window.handleAvatarUpload = handleAvatarUpload;
|
||||||
window.copyUserId = copyUserId;
|
window.copyUserId = copyUserId;
|
||||||
window.toggleSettingsMenu = toggleSettingsMenu;
|
window.toggleSettingsMenu = toggleSettingsMenu;
|
||||||
|
|||||||
@@ -106,6 +106,16 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div id="onlineUsersList" class="glass rounded-xl p-4 mb-6 hidden">
|
||||||
|
<div class="flex items-center justify-between mb-2">
|
||||||
|
<h3 class="text-sm font-medium text-gray-300">在线用户</h3>
|
||||||
|
<span id="onlineUsersSummary" class="text-xs text-gray-500">0 人在线</span>
|
||||||
|
</div>
|
||||||
|
<div id="usersContainer" class="max-h-56 overflow-y-auto space-y-3">
|
||||||
|
<!-- 在线用户将在这里动态生成 -->
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- WebSocket连接状态指示 -->
|
<!-- WebSocket连接状态指示 -->
|
||||||
<div id="wsStatus" class="mt-4 flex items-center justify-center gap-2 text-xs text-gray-500">
|
<div id="wsStatus" class="mt-4 flex items-center justify-center gap-2 text-xs text-gray-500">
|
||||||
<span id="wsStatusDot" class="w-2 h-2 bg-gray-500 rounded-full"></span>
|
<span id="wsStatusDot" class="w-2 h-2 bg-gray-500 rounded-full"></span>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import Offer from './offer';
|
|||||||
import Answer from './answer';
|
import Answer from './answer';
|
||||||
import Candidate from './candidate';
|
import Candidate from './candidate';
|
||||||
import { v4 as uuid } from 'uuid';
|
import { v4 as uuid } from 'uuid';
|
||||||
import { onGetAllConnectionIds } from './websockethandler';
|
import { onGetAllConnectionIds, onGetOnlineUsers as onGetWsOnlineUsers } from './websockethandler';
|
||||||
import { log, LogLevel } from '../log';
|
import { log, LogLevel } from '../log';
|
||||||
/**
|
/**
|
||||||
* 断开连接记录类
|
* 断开连接记录类
|
||||||
@@ -1106,6 +1106,66 @@ function getAllConnectionIds(req: Request, res: Response): void {
|
|||||||
// 返回JSON响应,包含连接ID列表和总数量
|
// 返回JSON响应,包含连接ID列表和总数量
|
||||||
res.json({ connectionIds: connectionIds, totalCount: connectionIds.length });
|
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处理器函数
|
* 导出HTTP处理器函数
|
||||||
*/
|
*/
|
||||||
@@ -1125,5 +1185,6 @@ export {
|
|||||||
postAnswer, // 处理answer信令消息
|
postAnswer, // 处理answer信令消息
|
||||||
postCandidate, // 处理candidate信令消息
|
postCandidate, // 处理candidate信令消息
|
||||||
onGetConnections, // 获取房间和用户信息
|
onGetConnections, // 获取房间和用户信息
|
||||||
getAllConnectionIds // 获取所有连接ID
|
getAllConnectionIds, // 获取所有连接ID
|
||||||
|
getOnlineUsers // 获取在线WebSocket用户列表
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -29,6 +29,21 @@ interface ConnectionGroup {
|
|||||||
participants: Set<WebSocket>;
|
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
|
* 键: connectionId
|
||||||
@@ -440,6 +455,53 @@ function onGetAllConnectionIds(): string[] {
|
|||||||
return connectionIds;
|
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对多模式)
|
* 处理chat-message信令(1对多模式)
|
||||||
* host的消息转发给所有participants,participant的消息转发给host
|
* host的消息转发给所有participants,participant的消息转发给host
|
||||||
@@ -451,6 +513,13 @@ function onMessage(ws: WebSocket, message: any): void {
|
|||||||
const connectionId = message.connectionId;
|
const connectionId = message.connectionId;
|
||||||
const chatMessage = message.message;
|
const chatMessage = message.message;
|
||||||
const senderParticipantId = (ws as any).participantId;
|
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.participantId = senderParticipantId;
|
||||||
chatMessage.connectionId = connectionId;
|
chatMessage.connectionId = connectionId;
|
||||||
if (connectionGroup.has(connectionId)) {
|
if (connectionGroup.has(connectionId)) {
|
||||||
@@ -476,4 +545,4 @@ function onMessage(ws: WebSocket, message: any): void {
|
|||||||
/**
|
/**
|
||||||
* 导出WebSocket处理器函数
|
* 导出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的路由
|
// 不需要会话ID的路由
|
||||||
router.get('/connection-ids', handler.getAllConnectionIds);
|
router.get('/connection-ids', handler.getAllConnectionIds);
|
||||||
|
router.get('/users', handler.getOnlineUsers);
|
||||||
|
|
||||||
// 需要会话ID的路由
|
// 需要会话ID的路由
|
||||||
router.use(handler.checkSessionId);
|
router.use(handler.checkSessionId);
|
||||||
|
|||||||
Reference in New Issue
Block a user