聊天:增加私聊部分逻辑
测试:增加测试脚本,减少socket断开的打印
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { STATUS } from './../consts/statusCode';
|
||||
import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './../db/PrivateMessage';
|
||||
import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage';
|
||||
import { genCode, resResult } from '../pubUtils/util';
|
||||
import { pinus } from 'pinus';
|
||||
import { MSG_CODE_LEN, MSG_STATUS, ON_MSG_ROUTE } from '../consts';
|
||||
import { getRoleOnlineInfo } from './redisService';
|
||||
|
||||
/**
|
||||
* @description 生成私聊房间号
|
||||
* @export
|
||||
* @param {string[]} roleIds
|
||||
* @returns
|
||||
*/
|
||||
export function privateRoomId(roleId: string, targetRoleId: string) {
|
||||
return [roleId, targetRoleId].sort().join('_');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 生成群聊房间号
|
||||
* @export
|
||||
* @param {string} channel
|
||||
* @param {string} channelId
|
||||
* @returns
|
||||
*/
|
||||
export function groupRoomId(channel: string, channelId: string) {
|
||||
return `${channel}_${channelId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 设置群消息和私聊消息的公共字段
|
||||
* @param {(PrivateMessageParam | GroupMessageParam)} msg
|
||||
*/
|
||||
function setupBaseMsgParm(msg: PrivateMessageParam | GroupMessageParam ) {
|
||||
msg.msgCode = genCode(MSG_CODE_LEN);
|
||||
msg.status = MSG_STATUS.NORMAL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 生成私聊消息数据
|
||||
* @param {string} roleId
|
||||
* @param {string} roleName
|
||||
* @param {number} type
|
||||
* @param {string} content
|
||||
* @param {string} targetRoleId
|
||||
* @param {string} targetMsgCode
|
||||
* @returns
|
||||
*/
|
||||
function createPrivateMsgData(roleId: string, roleName: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
|
||||
const result: PrivateMessageParam = {roleId, roleName, type, content, targetRoleId, targetMsgCode};
|
||||
setupBaseMsgParm(result);
|
||||
result.roomId = privateRoomId(roleId, targetRoleId);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 数据库中创建私聊数据
|
||||
* @export
|
||||
* @param {string} roleId
|
||||
* @param {string} roleName
|
||||
* @param {number} type
|
||||
* @param {string} content
|
||||
* @param {string} targetRoleId
|
||||
* @param {string} targetMsgCode
|
||||
* @returns
|
||||
*/
|
||||
export async function createPrivateMsg(roleId: string, roleName: string, type: number, content: string, targetRoleId: string, targetMsgCode: string) {
|
||||
const msgData: PrivateMessageParam = createPrivateMsgData(roleId, roleName, type, content, targetRoleId, targetMsgCode);
|
||||
const result: PrivateMessageType = await PrivateMessageModel.createMsg(msgData);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 给某个玩家发送消息
|
||||
* @export
|
||||
* @param {string} targetRoleId
|
||||
* @param {(PrivateMessageType | GroupMessageType)} msg
|
||||
*/
|
||||
export async function pushMsgToRole(targetRoleId: string, msg: PrivateMessageType | GroupMessageType) {
|
||||
const { sid } = await getRoleOnlineInfo(targetRoleId);
|
||||
if (sid) {
|
||||
pinus.app.get('channelService').pushMessageByUids(ON_MSG_ROUTE, resResult(STATUS.SUCCESS, msg), [{uid: targetRoleId, sid}]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user