Files
ZYZ/game-server/app/servers/chat/remote/chatRemote.ts
2022-09-23 17:29:38 +08:00

160 lines
4.9 KiB
TypeScript

import { Application, ChannelService, HandlerService, } from 'pinus';
import { reloadResources } from '../../../pubUtils/data';
import { _checkFilterWords, taflush } from '../../../services/sdkService';
import { getServerMainten, setServerMainten, stopServerMainten } from '../../../services/gmService';
import { errlogger } from '../../../util/logger';
import { addUserToChannel, sendMessageToChannel, sendMessgeToChannelByBatch } from '../../../services/pushService';
import { setApiIsClose } from '../../../services/chatService';
export default function (app: Application) {
new HandlerService(app, {});
return new ChatRemote(app);
}
// rpc 定义挪到单独的定义文件(user.rpc.define.ts)。解决ts-node 有可能找不到定义的问题。
// 你也可以用其它方法解决,或者没有遇到过这个问题的话,定义还是可以放在这里。
// UserRpc的命名空间自动合并
// declare global {
// interface UserRpc {
// chat: {
// chatRemote: RemoterClass<FrontendSession, ChatRemote>;
// };
// }
// }
export class ChatRemote {
constructor(private app: Application) {
this.app = app;
this.channelService = app.get('channelService');
// getTire();
}
private channelService: ChannelService;
/**
* @description 添加用户到频道
* @param {string} channelName 频道名
* @param {string} roleId 要添加的用户名
* @param {string} sid 用户连接的前端服务器名
* @memberof ChatRemote
*/
public async addChannel(channelName: string, roleId: string, sid: string) {
try {
let channel = this.channelService.getChannel(channelName, true);
if (!channel) return;
addUserToChannel(channel, roleId, sid);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* @description 从频道中删除用户
* @param {string} channelName
* @param {string} roleId
* @param {string} sid
* @memberof ChatRemote
*/
public async leaveChannel(channelName: string, roleId: string, sid: string) {
try {
let channel = this.channelService.getChannel(channelName, false);
// leave channel
if (!channel) return;
channel.leave(roleId, sid);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 向频道推送消息
* @param roomId 房间号
* @param path 推送地址
* @param data 推送数据
* @returns
*/
public async pushMessage(roomId: string, path: string, data: any, isBatch = false) {
try {
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
if(isBatch) { // 分批推送
sendMessgeToChannelByBatch(channel, path, data);
} else {
sendMessageToChannel(channel, path, data);
}
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async deleteChannel(roomId: string) {
try {
let channel = this.channelService.getChannel(roomId, false);
if (!channel) return;
channel.destroy();
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
/**
* 重载json资源
*/
public async reloadResources() {
try {
reloadResources();
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async checkFilterWords(word: string) {
try {
return await _checkFilterWords(word);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public setServerMainten(serverIds: number[], startTime: number, endTime: number) {
try {
setServerMainten(serverIds, startTime, endTime);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public stopServerMainten(serverIds: number[]) {
try {
stopServerMainten(serverIds);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public getServerMainten(serverId: number) {
try {
return getServerMainten(serverId);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public taflush() {
try {
return taflush();
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
public async setApiIsClose(isClose: boolean) {
try {
setApiIsClose(isClose);
} catch(e) {
errlogger.error(`remote ${__filename} \n ${e.stack}`);
}
}
}