【m】log完成
This commit is contained in:
@@ -8,6 +8,7 @@ import Answer from './answer';
|
||||
import Candidate from './candidate';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import { onGetAllConnectionIds } from './websockethandler';
|
||||
import { log, LogLevel } from '../log';
|
||||
/**
|
||||
* 断开连接记录类
|
||||
* 用于记录断开连接的信息
|
||||
@@ -226,7 +227,7 @@ function _checkForTimedOutSessions(): void {
|
||||
continue;
|
||||
// 删除超时会话
|
||||
_deleteSession(sessionId);
|
||||
console.log(`deleted sessionId:${sessionId} by timeout.`);
|
||||
log(LogLevel.log, `deleted sessionId:${sessionId} by timeout.`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -757,7 +758,7 @@ function createConnection(req: Request, res: Response): void {
|
||||
// 检查连接ID是否已被使用
|
||||
if (pair[0] != null && pair[1] != null) {
|
||||
const err = new Error(`${connectionId}: This connection id is already used.`);
|
||||
console.log(err);
|
||||
log(LogLevel.warn, err.message);
|
||||
res.status(400).send({ error: err });
|
||||
return;
|
||||
} else if (pair[0] != null) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import Offer from './offer';
|
||||
import Answer from './answer';
|
||||
import Candidate from './candidate';
|
||||
import { log, LogLevel } from '../log';
|
||||
|
||||
/**
|
||||
* 是否为私有模式
|
||||
@@ -73,7 +74,7 @@ function add(ws: WebSocket): void {
|
||||
const id = new Set<string>();
|
||||
clients.set(ws, id);
|
||||
// 记录添加WebSocket连接的日志
|
||||
console.log(`Add WebSocket: ${ws.url}`);
|
||||
log(LogLevel.log, `Add WebSocket: ${ws.url}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,7 +130,7 @@ function remove(ws: WebSocket): void {
|
||||
group.host.send(JSON.stringify({ type: "participant-left", connectionId: connectionId, participantId: (ws as any).participantId }));
|
||||
}
|
||||
}
|
||||
console.log(`Remove connectionId: ${connectionId}`);
|
||||
log(LogLevel.log, `Remove connectionId: ${connectionId}`);
|
||||
});
|
||||
|
||||
clients.delete(ws);
|
||||
@@ -150,13 +151,13 @@ function onConnect(ws: WebSocket, connectionId: string): void {
|
||||
if (connectionGroup.has(connectionId)) {
|
||||
const group = connectionGroup.get(connectionId);
|
||||
group.participants.add(ws);
|
||||
console.log(`Participant ${participantId} joined connectionId: ${connectionId}, total participants: ${group.participants.size}`);
|
||||
log(LogLevel.log, `Participant ${participantId} joined connectionId: ${connectionId}, total participants: ${group.participants.size}`);
|
||||
// 通知host有新participant加入
|
||||
group.host.send(JSON.stringify({ type: "participant-joined", connectionId: connectionId, participantId: participantId }));
|
||||
} else {
|
||||
connectionGroup.set(connectionId, { host: ws, participants: new Set<WebSocket>() });
|
||||
polite = false;
|
||||
console.log(`Host created connectionId: ${connectionId}`);
|
||||
log(LogLevel.log, `Host created connectionId: ${connectionId}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,12 +189,12 @@ function onDisconnect(ws: WebSocket, connectionId: string): void {
|
||||
participantWs.send(JSON.stringify({ type: "disconnect", connectionId: connectionId, reason: "host-left" }));
|
||||
});
|
||||
connectionGroup.delete(connectionId);
|
||||
console.log(`Host disconnected, room ${connectionId} deleted, notified ${group.participants.size} participants`);
|
||||
log(LogLevel.log, `Host disconnected, room ${connectionId} deleted, notified ${group.participants.size} participants`);
|
||||
} else {
|
||||
// participant断开连接,从组中移除并通知host(使用participant-left类型,host不会关闭房间)
|
||||
group.participants.delete(ws);
|
||||
group.host.send(JSON.stringify({ type: "participant-left", connectionId: connectionId, participantId: (ws as any).participantId }));
|
||||
console.log(`Participant left connectionId: ${connectionId}, remaining participants: ${group.participants.size}`);
|
||||
log(LogLevel.log, `Participant left connectionId: ${connectionId}, remaining participants: ${group.participants.size}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,7 +202,7 @@ function onDisconnect(ws: WebSocket, connectionId: string): void {
|
||||
ws.send(JSON.stringify({ type: "disconnect", connectionId: connectionId }));
|
||||
//RemoveHeartbeat(ws);
|
||||
// 记录断开连接的日志
|
||||
console.log(`Disconnect connectionId: ${connectionId}`);
|
||||
log(LogLevel.log, `Disconnect connectionId: ${connectionId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -409,14 +410,14 @@ function AddHeartbeat(ws: WebSocket, connectionId: string) {
|
||||
const now = Date.now();
|
||||
// 检查上次活动时间,如果超过60秒没有活动,关闭连接
|
||||
if (now - (ws as any).lastActivity > 10000) {
|
||||
console.log('WebSocket connection timeout, closing...');
|
||||
log(LogLevel.warn, 'WebSocket connection timeout, closing...');
|
||||
clearInterval((ws as any).heartbeatTimer);
|
||||
//ws.close();
|
||||
onDisconnect(ws, connectionId);
|
||||
} else {
|
||||
// 发送ping消息
|
||||
ws.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: { type: "ping"} }));
|
||||
console.log('WebSocket connection heartbeat, lastActivity: ', (ws as any).lastActivity);
|
||||
log(LogLevel.log, 'WebSocket connection heartbeat, lastActivity: ', (ws as any).lastActivity);
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user