【m】远端视频开发

This commit is contained in:
2026-03-04 22:29:10 +08:00
parent a5d9368ae1
commit 957ab561bf
7 changed files with 156 additions and 57 deletions

View File

@@ -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';
/**
* 断开连接记录类
* 用于记录断开连接的信息
@@ -1073,6 +1073,38 @@ function onGetConnections(req: Request, res: Response): void {
res.json({ rooms: rooms, totalRooms: rooms.length });
}
/**
* @swagger
* /signaling/connection-ids:
* get:
* summary: 获取所有连接ID
* description: 获取所有当前活跃的连接ID
* security:
* - sessionAuth: []
* responses:
* 200:
* description: 成功获取连接ID列表
* content:
* application/json:
* schema:
* type: object
* properties:
* connectionIds:
* type: array
* items:
* type: string
* description: 连接ID
* totalCount:
* type: number
* description: 总连接数
*/
function getAllConnectionIds(req: Request, res: Response): void {
// 获取所有连接ID
const connectionIds = onGetAllConnectionIds();
// 返回JSON响应包含连接ID列表和总数量
res.json({ connectionIds: connectionIds, totalCount: connectionIds.length });
}
/**
* 导出HTTP处理器函数
*/
@@ -1091,5 +1123,6 @@ export {
postOffer, // 处理offer信令消息
postAnswer, // 处理answer信令消息
postCandidate, // 处理candidate信令消息
onGetConnections // 获取房间和用户信息
onGetConnections, // 获取房间和用户信息
getAllConnectionIds // 获取所有连接ID
};

View File

@@ -60,7 +60,7 @@ function reset(mode: string): void {
*/
function add(ws: WebSocket): void {
// 为新连接创建空的连接ID集合
var id = new Set<string>();
const id = new Set<string>();
clients.set(ws, id);
// 记录添加WebSocket连接的日志
console.log(`Add WebSocket: ${id}`);
@@ -347,7 +347,22 @@ function onCandidate(ws: WebSocket, message: any): void {
clearInterval((ws as any).heartbeatTimer);
}
}
/**
* 处理获取所有连接ID的请求
* @param ws WebSocket连接实例
*/
function onGetAllConnectionIds(): string[] {
// 获取所有connectionId
const connectionIds = Array.from(connectionPair.keys());
// 发送连接ID列表给客户端
// ws.send(JSON.stringify({
// type: "connection-ids",
// connectionIds: connectionIds
// }));
return connectionIds;
}
/**
* 导出WebSocket处理器函数
*/
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate,onCallConnectionId, onBroadcast, AddHeartbeat, RemoveHeartbeat };
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, onBroadcast, onGetAllConnectionIds, AddHeartbeat, RemoveHeartbeat };

View File

@@ -2,6 +2,11 @@ import * as express from 'express';
import * as handler from'./class/httphandler';
const router: express.Router = express.Router();
// 不需要会话ID的路由
router.get('/connection-ids', handler.getAllConnectionIds);
// 需要会话ID的路由
router.use(handler.checkSessionId);
router.get('/connection', handler.getConnection);
router.get('/offer', handler.getOffer);