From 7d85f554f4ee77d06ba45ce03f52494f5d8ffe89 Mon Sep 17 00:00:00 2001 From: ciniao <4041990@qq.com> Date: Thu, 11 Jan 2024 16:06:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E5=88=86=E7=BB=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/public/chat.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/public/chat.ts b/src/public/chat.ts index 877f6fd..4787ef0 100644 --- a/src/public/chat.ts +++ b/src/public/chat.ts @@ -4,6 +4,7 @@ import { MsgChat } from '../shared/protocols/msg_s2c/MsgChat'; import { player } from '../shared/protocols/user/type'; import { chatLog, chatMsgLog } from '../shared/protocols/type' import { PublicShared } from '../shared/public/public'; +import { clusterRunOnce } from '../clusterUtils'; const msgListLen = { 'cross': 30, @@ -28,13 +29,9 @@ export function getCrossChatGroupByOpenDay(){ export class ChatFun { /**新增消息 */ static async newMsg(sendData: MsgChat) { - G.mongodb.collection('chat').updateOne( - { type: `${sendData.type}${sendData.type == 'guild' ? (sendData.sender as player)?.ghId : ''}` }, - { $push: { list: { $each: [sendData], $slice: -msgListLen[sendData.type] } } }, - { upsert: true } - ); - + let addToDB = 0; if (sendData.type == 'guild') { + addToDB = 1; G.server.broadcastClusterMsg('msg_s2c/Chat', sendData,{ghId:(sendData.sender as player)?.ghId}); }else if(sendData.type == 'cross'){ //所有的子进程都会收到,不需要集群内分别广播 @@ -42,9 +39,27 @@ export class ChatFun { //如果时候同一个分组的 delete sendData?.otherData?.group; G.server.broadcastMsg('msg_s2c/Chat', sendData); + addToDB = 2; } }else{ G.server.broadcastClusterMsg('msg_s2c/Chat', sendData); + addToDB = 1; + } + + if(addToDB == 1){ + G.mongodb.collection('chat').updateOne( + { type: `${sendData.type}${sendData.type == 'guild' ? (sendData.sender as player)?.ghId : ''}` }, + { $push: { list: { $each: [sendData], $slice: -msgListLen[sendData.type] } } }, + { upsert: true } + ); + }else if(addToDB == 2){ + clusterRunOnce(()=>{ + G.mongodb.collection('chat').updateOne( + { type: `${sendData.type}${sendData.type == 'guild' ? (sendData.sender as player)?.ghId : ''}` }, + { $push: { list: { $each: [sendData], $slice: -msgListLen[sendData.type] } } }, + { upsert: true } + ); + }) } }