From 52b5faf5a78a2d40e588c17a84f210c783c7e09f Mon Sep 17 00:00:00 2001 From: stary <834207172@qq.com> Date: Mon, 18 May 2026 23:29:48 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E8=B5=B7=E9=80=9A=E8=AF=9D=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/class/websockethandler.ts | 53 ++++++++++++++++++++++++++++++++++- src/websocket.ts | 6 ++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/class/websockethandler.ts b/src/class/websockethandler.ts index 135b98a..96fd923 100644 --- a/src/class/websockethandler.ts +++ b/src/class/websockethandler.ts @@ -378,6 +378,11 @@ function onCallConnectionId(ws: WebSocket, message: any): void { }); } } +/** + * 处理host-userInfo消息,更新host的用户信息 + * @param ws WebSocket连接实例 + * @param message 消息数据 + */ function onHostUserInfo(ws: WebSocket, message: any): void { (ws as any).userInfo = { id: message.id || '', @@ -387,7 +392,51 @@ function onHostUserInfo(ws: WebSocket, message: any): void { log(LogLevel.log, 'Updated current ws userInfo:', (ws as any).userInfo); } +function onInviteCall(ws: WebSocket, message: any): void { + const connectionId = message.connectionId as string; + const targetSocketId = message.targetSocketId as string; + const targetUserId = message.targetUserId as string; + let targetWs: WebSocket = null; + clients.forEach((_connectionIds, clientWs) => { + if (targetWs || clientWs === ws) { + return; + } + + const userInfo = ((clientWs as any).userInfo || {}) as UserInfo; + if ((targetSocketId && (clientWs as any).socketId === targetSocketId) || + (targetUserId && userInfo.id === targetUserId)) { + targetWs = clientWs; + } + }); + + if (!targetWs) { + ws.send(JSON.stringify({ + type: 'invite-failed', + data: { + connectionId, + reason: 'target-offline' + } + })); + log(LogLevel.warn, `invite-call target not found: socketId=${targetSocketId}, userId=${targetUserId}`); + return; + } + + targetWs.send(JSON.stringify({ + type: 'invite-call', + data: { + connectionId, + inviterSocketId: (ws as any).socketId || '', + inviterUserId: message.inviterUserId || (((ws as any).userInfo || {}) as UserInfo).id || '', + inviterName: message.inviterName || (((ws as any).userInfo || {}) as UserInfo).name || '邀请方', + inviterAvatar: message.inviterAvatar || (((ws as any).userInfo || {}) as UserInfo).avatar || '', + targetSocketId: targetSocketId || '', + targetUserId: targetUserId || '' + } + })); + + log(LogLevel.log, `Forwarded invite-call to socketId=${targetSocketId}, userId=${targetUserId}, connectionId=${connectionId}`); +} /** * 处理广播消息请求(1对多模式) * @param ws WebSocket连接实例 @@ -563,4 +612,6 @@ function onMessage(ws: WebSocket, message: any): void { /** * 导出WebSocket处理器函数 */ -export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, onBroadcast, onGetAllConnectionIds, onGetOnlineUsers, AddHeartbeat, RemoveHeartbeat, onMessage, isHost, broadcastToGroup, connectionGroup, onHostUserInfo }; +export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, + onBroadcast, onGetAllConnectionIds, onGetOnlineUsers, AddHeartbeat, RemoveHeartbeat, onMessage, isHost, + broadcastToGroup, connectionGroup, onHostUserInfo, onInviteCall }; diff --git a/src/websocket.ts b/src/websocket.ts index 33f7d46..5b78278 100644 --- a/src/websocket.ts +++ b/src/websocket.ts @@ -108,9 +108,9 @@ export default class WSSignaling { case 'host-userInfo': handler.onHostUserInfo(ws, msg.data); break; - // case 'invite-call': - // handler.onInviteCall(ws, msg.data); - // break; + case 'invite-call': + handler.onInviteCall(ws, msg.data); + break; // case 'invite-accepted': // handler.onInviteAccepted(ws, msg.data); // break;