Files
ZYZ/game-server/app/servers/chat/handler/chatHandler.ts
2020-12-09 17:23:15 +08:00

71 lines
2.1 KiB
TypeScript

import {Application, BackendSession} from 'pinus';
import { HeroModel } from '../../../db/Hero';
import Actor from '../../../pubUtils/actor';
export default function(app: Application) {
return new ChatHandler(app);
}
export class ChatHandler {
constructor(private app: Application) {
}
/**
* Send messages to users
*
* @param {Object} msg message from client
* @param {Object} session
*
*/
async send(msg: {content: string , target: string}, session: BackendSession) {
let rid = session.get('rid');
let username = session.uid.split('*')[0];
let channelService = this.app.get('channelService');
let param = {
msg: msg.content,
from: username,
target: msg.target
};
let channel = channelService.getChannel(rid, false);
// the target is all users
if (msg.target === '*') {
channel.pushMessage('onChat', param);
}
// the target is specific user
else {
let tuid = msg.target + '*' + rid;
let tsid = channel.getMember(tuid)['sid'];
channelService.pushMessageByUids('onChat', param, [{
uid: tuid,
sid: tsid
}]);
}
}
async send2(msg: {content: string , target: string}, session: BackendSession) {
let rid = session.get('rid');
let username = session.uid.split('*')[0];
let channelService = this.app.get('channelService');
let param = {
msg: msg.content,
from: username,
target: msg.target
};
let channel = channelService.getChannel(rid, false);
// the target is all users
if (msg.target === '*') {
channel.pushMessage('onChat', param);
}
// the target is specific user
else {
let tuid = msg.target + '*' + rid;
let tsid = channel.getMember(tuid)['sid'];
channelService.pushMessageByUids('onChat', param, [{
uid: tuid,
sid: tsid
}]);
}
}
}