【m】开始尝试接入后端

This commit is contained in:
zhangzheng
2026-03-04 17:55:55 +08:00
parent fd00100808
commit 93b56da25e
11 changed files with 681 additions and 217 deletions

View File

@@ -60,9 +60,10 @@ function reset(mode: string): void {
*/
function add(ws: WebSocket): void {
// 为新连接创建空的连接ID集合
clients.set(ws, new Set<string>());
var id = new Set<string>();
clients.set(ws, id);
// 记录添加WebSocket连接的日志
console.log(`Add WebSocket: ${ws}`);
console.log(`Add WebSocket: ${id}`);
}
/**
@@ -157,8 +158,8 @@ function onDisconnect(ws: WebSocket, connectionId: string): void {
// 向当前连接发送断开连接消息
ws.send(JSON.stringify({ type: "disconnect", connectionId: connectionId }));
//RemoveHeartbeat(ws);
// 记录断开连接的日志
console.log(`Disconnect connectionId: ${connectionId}`);
// 记录断开连接的日志
console.log(`Disconnect connectionId: ${connectionId}`);
}
/**
@@ -257,65 +258,71 @@ function onCandidate(ws: WebSocket, message: any): void {
}
return;
}
// 公共模式向所有其他客户端广播candidate
clients.forEach((_v, k) => {
if (k === ws) {
return;
}
k.send(JSON.stringify({ from: connectionId, to: "", type: "candidate", data: candidate }));
});
}
/**
* 处理广播消息请求
* @param ws WebSocket连接实例
* @param message 消息数据
*/
/**
* 处理广播消息请求
* @param ws WebSocket连接实例
* @param message 消息数据
*/
function onBroadcast(ws: WebSocket, message: any): void {
const broadcastMessage = message.message;
const targetConnectionId = message.targetConnectionId;
if (targetConnectionId) {
// 向指定连接广播
if (connectionPair.has(targetConnectionId)) {
const pair = connectionPair.get(targetConnectionId);
// 向连接对中的两个WebSocket实例发送消息
if (pair[0]) {
pair[0].send(JSON.stringify({
type: "broadcast",
message: broadcastMessage,
from: "server"
}));
}
if (pair[1]) {
pair[1].send(JSON.stringify({
type: "broadcast",
message: broadcastMessage,
from: "server"
}));
}
}
} else {
// 全局广播:向所有客户端发送消息
function onCallConnectionId(ws: WebSocket, message: any): void {
// 获取连接ID
const connectionId = message.connectionId;
const clientId = message.clientId;
clients.forEach((_v, k) => {
k.send(JSON.stringify({
type: "broadcast",
message: broadcastMessage,
from: "server"
}));
if (k === ws) {
return;
}
if (_v == clientId) {
k.send(JSON.stringify({ from: connectionId, to: "", type: "call-request", data: connectionId }));
}
});
}
}
function AddHeartbeat(ws: WebSocket, connectionId: string){
// 初始化心跳检测
/**
* 处理广播消息请求
* @param ws WebSocket连接实例
* @param message 消息数据
*/
/**
* 处理广播消息请求
* @param ws WebSocket连接实例
* @param message 消息数据
*/
function onBroadcast(ws: WebSocket, message: any): void {
const broadcastMessage = message.message;
const targetConnectionId = message.targetConnectionId;
if (targetConnectionId) {
// 向指定连接广播
if (connectionPair.has(targetConnectionId)) {
const pair = connectionPair.get(targetConnectionId);
// 向连接对中的两个WebSocket实例发送消息
if (pair[0]) {
pair[0].send(JSON.stringify({
type: "broadcast",
message: broadcastMessage,
from: "server"
}));
}
if (pair[1]) {
pair[1].send(JSON.stringify({
type: "broadcast",
message: broadcastMessage,
from: "server"
}));
}
}
} else {
// 全局广播:向所有客户端发送消息
clients.forEach((_v, k) => {
k.send(JSON.stringify({
type: "broadcast",
message: broadcastMessage,
from: "server"
}));
});
}
}
function AddHeartbeat(ws: WebSocket, connectionId: string) {
// 初始化心跳检测
(ws as any).lastActivity = Date.now();
// 设置心跳检测定时器每30秒发送一次ping
@@ -333,14 +340,14 @@ function AddHeartbeat(ws: WebSocket, connectionId: string){
console.log('WebSocket connection heartbeat, lastActivity: ', (ws as any).lastActivity);
}
}, 3000);
}
function RemoveHeartbeat(ws: WebSocket){
}
function RemoveHeartbeat(ws: WebSocket) {
// 清除心跳检测定时器
if ((ws as any).heartbeatTimer) {
clearInterval((ws as any).heartbeatTimer);
}
}
/**
* 导出WebSocket处理器函数
*/
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onBroadcast, AddHeartbeat, RemoveHeartbeat };
}
/**
* 导出WebSocket处理器函数
*/
export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate,onCallConnectionId, onBroadcast, AddHeartbeat, RemoveHeartbeat };

View File

@@ -104,6 +104,10 @@ export default class WSSignaling {
case "broadcast":
handler.onBroadcast(ws, msg.data);
break;
case 'call-request'://接受连接ConnectionId
// 处理callConnectionId信令
handler.onCallConnectionId(ws, msg.data);
break;
default:
// 忽略未知消息类型
break;