聊天:增加私聊部分逻辑
测试:增加测试脚本,减少socket断开的打印
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {Application, BackendSession} from 'pinus';
|
||||
import { resResult } from '../../../pubUtils/util';
|
||||
import { STATUS } from '../../../consts';
|
||||
import { createPrivateMsg, pushMsgToRole } from '../../../services/chatService';
|
||||
|
||||
|
||||
export default function(app: Application) {
|
||||
@@ -76,8 +77,8 @@ export class ChatHandler {
|
||||
* @param {BackendSession} session
|
||||
* @memberof ChatHandler
|
||||
*/
|
||||
async sendGroupMessage(msg: { channel: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
|
||||
const { channel, type, content, targetRoleId, targetMsgCode } = msg;
|
||||
async sendGroupMessage(msg: { channel: string, channelId: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
|
||||
const { channel, channelId, type, content, targetRoleId, targetMsgCode } = msg;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,6 +89,12 @@ export class ChatHandler {
|
||||
*/
|
||||
async sendPrivateMessage(msg: { type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
|
||||
const { type, content, targetRoleId, targetMsgCode } = msg;
|
||||
const roleId = session.get('roleId');
|
||||
const roleName = session.get('roleName');
|
||||
const msgData = await createPrivateMsg(roleId, roleName, type, content, targetRoleId, targetMsgCode);
|
||||
await pushMsgToRole(targetRoleId, msgData);
|
||||
if (!msgData) return resResult(STATUS.WRONG_PARMS);
|
||||
return resResult(STATUS.SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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}]);
|
||||
}
|
||||
}
|
||||
Executable
+3
@@ -0,0 +1,3 @@
|
||||
cp ./test/PinusWSClient.ts ./node_modules/pinus-robot-plugin/src/PinusWSClient.ts
|
||||
cp ./test/PinusWSClient.js ./node_modules/pinus-robot-plugin/dist/PinusWSClient.js
|
||||
./node_modules/mocha/bin/mocha -t 5000 -r ts-node/register ./test/**test.ts
|
||||
@@ -139,7 +139,7 @@ class PinusWSClient {
|
||||
this.send(this._package.encode(Package.TYPE_HANDSHAKE, Protocol.strencode(JSON.stringify(this.handshakeBuffer))));
|
||||
}
|
||||
onClose(e) {
|
||||
console.error('[Pinus] connect close:', e);
|
||||
console.error('[Pinus] connect close');
|
||||
// this.emit(Pinus.EVENT_CLOSE,e);
|
||||
}
|
||||
onIOError(e) {
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { MSG_TYPE, ON_MSG_ROUTE } from './../app/consts';
|
||||
import { indexOf } from 'underscore';
|
||||
import { COM_TEAM_STATUS, DEFAULT_HEROES } from './../../shared/consts/consts';
|
||||
import { Client } from './Client';
|
||||
import { COM_BTL_QUALITY } from './../app/consts/constModules/itemConst';
|
||||
import 'mocha';
|
||||
import { PinusWSClient } from 'pinus-robot-plugin';
|
||||
import { expect } from 'chai';
|
||||
import { checkDisplayItems, checkSuccessResponse } from './CheckPatten';
|
||||
|
||||
const TEXT_MSG = {type: MSG_TYPE.TEXT, content: 'hello world'}
|
||||
|
||||
function sendPrivateMessageParm(targetRoleInfo, msg) {
|
||||
const { roleId: targetRoleId, roleName: targetRoleName } = targetRoleInfo;
|
||||
const result = {targetRoleId, targetRoleName, ...msg};
|
||||
return result;
|
||||
}
|
||||
|
||||
describe('私聊消息发送和接收', function() {
|
||||
let pinusClient: PinusWSClient;
|
||||
let pinusClientT: PinusWSClient; // 用做测试队友
|
||||
let roleInfo;
|
||||
let roleInfoT;
|
||||
|
||||
beforeEach(function(done) {
|
||||
const c = new Client();
|
||||
const cT = new Client('13121622738');
|
||||
const timer = setInterval(() => {
|
||||
if (c.client && cT.client) {
|
||||
pinusClient = c.client;
|
||||
roleInfo = c.roleInfo;
|
||||
pinusClientT = cT.client;
|
||||
roleInfoT = cT.roleInfo;
|
||||
clearInterval(timer);
|
||||
done();
|
||||
}
|
||||
}, 500);
|
||||
});
|
||||
|
||||
afterEach(function(done) {
|
||||
pinusClient.disconnect();
|
||||
pinusClientT.disconnect();
|
||||
done();
|
||||
});
|
||||
|
||||
it('两个玩家互相发送', function(done) {
|
||||
pinusClientT.on(ON_MSG_ROUTE, (msg) => {
|
||||
console.log(ON_MSG_ROUTE , msg);
|
||||
checkSuccessResponse(msg);
|
||||
expect(msg.data.content).to.equal(TEXT_MSG.content);
|
||||
});
|
||||
|
||||
pinusClient.request('chat.chatHandler.sendPrivateMessage', sendPrivateMessageParm(roleInfoT, TEXT_MSG), (res) => {
|
||||
checkSuccessResponse(res, false);
|
||||
setTimeout(() => {
|
||||
done();
|
||||
}, 200);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user