【m】增加swagger

This commit is contained in:
2026-03-01 23:41:42 +08:00
parent f9037ca05d
commit c39dbfe8d5
11 changed files with 1205 additions and 104 deletions

View File

@@ -127,6 +127,8 @@ function onConnect(ws: WebSocket, connectionId: string): void {
connectionIds.add(connectionId);
// 发送连接成功消息
ws.send(JSON.stringify({ type: "connect", connectionId: connectionId, polite: polite }));
//启用心跳包
//AddHeartbeat(ws, connectionId);
}
/**
@@ -154,6 +156,9 @@ function onDisconnect(ws: WebSocket, connectionId: string): void {
connectionPair.delete(connectionId);
// 向当前连接发送断开连接消息
ws.send(JSON.stringify({ type: "disconnect", connectionId: connectionId }));
//RemoveHeartbeat(ws);
// 记录断开连接的日志
console.log(`Disconnect connectionId: ${connectionId}`);
}
/**
@@ -262,30 +267,7 @@ function onCandidate(ws: WebSocket, message: any): void {
});
}
/**
* 处理获取连接信息请求
* @param ws WebSocket连接实例
*/
function onGetConnections(ws: WebSocket): void {
// 收集所有connectionId
const allConnectionIds = Array.from(connectionPair.keys());
// 收集所有WebSocket连接信息
const allWebSockets = Array.from(clients.entries()).map(([ws, connectionIds]) => {
return {
connectionIds: Array.from(connectionIds),
// 注意这里不能直接序列化WebSocket对象只能返回连接数量或其他信息
connected: true
};
});
// 发送连接信息给请求的客户端
ws.send(JSON.stringify({
type: "connections",
connectionIds: allConnectionIds,
websocketCount: clients.size
}));
}
/**
* 处理广播消息请求
@@ -332,7 +314,7 @@ function onBroadcast(ws: WebSocket, message: any): void {
});
}
}
function AddHeartbeat(ws: WebSocket){
function AddHeartbeat(ws: WebSocket, connectionId: string){
// 初始化心跳检测
(ws as any).lastActivity = Date.now();
@@ -343,7 +325,8 @@ function AddHeartbeat(ws: WebSocket){
if (now - (ws as any).lastActivity > 10000) {
console.log('WebSocket connection timeout, closing...');
clearInterval((ws as any).heartbeatTimer);
ws.close();
//ws.close();
onDisconnect(ws, connectionId);
} else {
// 发送ping消息
ws.send(JSON.stringify({ type: "ping" }));
@@ -360,4 +343,4 @@ function RemoveHeartbeat(ws: WebSocket){
/**
* 导出WebSocket处理器函数
*/
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onGetConnections, onBroadcast, AddHeartbeat, RemoveHeartbeat };
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onBroadcast, AddHeartbeat, RemoveHeartbeat };