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 } + ); + }) } }