【m】增加swagger
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -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 };
|
||||
|
||||
@@ -6,11 +6,12 @@ import signaling from './signaling';
|
||||
import { log, LogLevel } from './log';
|
||||
import Options from './class/options';
|
||||
import { reset as resetHandler }from './class/httphandler';
|
||||
import { initSwagger } from './swagger';
|
||||
|
||||
const cors = require('cors');
|
||||
|
||||
export const createServer = (config: Options): express.Application => {
|
||||
const app: express.Application = express();
|
||||
export const createServer = (config: Options): express.Express => {
|
||||
const app: express.Express = express();
|
||||
resetHandler(config.mode);
|
||||
// logging http access
|
||||
if (config.logging != "none") {
|
||||
@@ -35,5 +36,8 @@ export const createServer = (config: Options): express.Application => {
|
||||
}
|
||||
});
|
||||
});
|
||||
// 初始化Swagger
|
||||
initSwagger(app, config);
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
64
WebApp/src/swagger.ts
Normal file
64
WebApp/src/swagger.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Swagger配置文件
|
||||
* 用于设置API文档的基本信息和路由
|
||||
*/
|
||||
import * as swaggerJSDoc from 'swagger-jsdoc';
|
||||
import * as swaggerUi from 'swagger-ui-express';
|
||||
import { Express } from 'express';
|
||||
import Options from './class/options';
|
||||
|
||||
/**
|
||||
* 初始化Swagger
|
||||
* @param app Express应用实例
|
||||
* @param config 配置选项
|
||||
*/
|
||||
export const initSwagger = (app: Express, config: Options): void => {
|
||||
// 根据配置生成服务器URL
|
||||
const protocol = config.secure ? 'https' : 'http';
|
||||
const port = config.port || 8080;
|
||||
const serverUrl = `${protocol}://localhost:${port}`;
|
||||
|
||||
/**
|
||||
* Swagger配置选项
|
||||
*/
|
||||
const swaggerOptions = {
|
||||
definition: {
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
title: 'WebRTC Signaling API',
|
||||
version: '1.0.0',
|
||||
description: 'WebRTC信令服务器API文档',
|
||||
contact: {
|
||||
name: 'WebRTC Team',
|
||||
email: 'contact@webrtc.example.com'
|
||||
}
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
url: serverUrl,
|
||||
description: '本地开发服务器'
|
||||
}
|
||||
],
|
||||
components: {
|
||||
securitySchemes: {
|
||||
sessionAuth: {
|
||||
type: 'apiKey',
|
||||
in: 'header',
|
||||
name: 'session-id',
|
||||
description: '会话ID'
|
||||
}
|
||||
}
|
||||
},
|
||||
security: [
|
||||
{
|
||||
sessionAuth: []
|
||||
}
|
||||
]
|
||||
},
|
||||
apis: ['./src/class/httphandler.ts', './src/signaling.ts']
|
||||
};
|
||||
|
||||
const swaggerSpec = swaggerJSDoc(swaggerOptions);
|
||||
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
|
||||
console.log(`Swagger文档已初始化,访问 ${serverUrl}/api-docs 查看`);
|
||||
};
|
||||
@@ -26,14 +26,14 @@ export default class WSSignaling {
|
||||
this.wss.on('connection', (ws: WebSocket) => {
|
||||
// 添加新的WebSocket连接到处理器
|
||||
handler.add(ws);
|
||||
handler.AddHeartbeat(ws);
|
||||
//handler.AddHeartbeat(ws);
|
||||
/**
|
||||
* 监听连接关闭事件
|
||||
*/
|
||||
ws.onclose = (): void => {
|
||||
// 从处理器中移除关闭的连接
|
||||
handler.remove(ws);
|
||||
handler.RemoveHeartbeat(ws);
|
||||
//handler.RemoveHeartbeat(ws);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -104,9 +104,6 @@ export default class WSSignaling {
|
||||
case "broadcast":
|
||||
handler.onBroadcast(ws, msg.data);
|
||||
break;
|
||||
case "onGetConnections":
|
||||
handler.onGetConnections(ws);
|
||||
break;
|
||||
default:
|
||||
// 忽略未知消息类型
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user