const Code = require("../../../../../shared/code"); module.exports = function (app) { return new ChatHandler(app); }; class ChatHandler { constructor(app) { this.app = app; this.channelService = this.app.get('channelService'); } /** * 加入世界频道 * @param {*} msg * @param {*} session * @param {*} next * @returns */ async joinWorldChannel(msg, session, next) { const uid = session.uid; const channelId = 'worldChannel'; const channel = this.channelService.getChannel(channelId, true); if (channel.getMember(uid)) { next(null, {code:Code.CHAT.FA_USER_ALREAD_JOINED, msg: "User already joined the world channel"}); return; } channel.add(session.uid, session.get('sid')); next(null, { code: Code.OK }); } /** * 离开世界频道 * @param {*} msg * @param {*} session * @param {*} next */ async leaveWorldChannel(msg, session, next) { const username = session.get('username'); const channelId = 'worldChannel'; const channel = this.channelService.getChannel(channelId, false); if (channel) { channel.leave(session.uid, session.get('sid')); } next(null, { code: Code.OK }); } // 加入队伍频道 async joinTeamChannel(msg, session, next) { const uid = session.uid; const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; const channel = this.channelService.getChannel(channelId, true); if (channel.getMember(uid)) { next(null, {code:Code.CHAT.FA_USER_ALREAD_JOINED, msg: "User already joined the team channel"}); return; } channel.add(session.uid, session.get('sid')); next(null, { code: 200 }); channel.pushMessage({ route: 'onAdd', user: uid, }); } // 离开队伍频道 async leaveTeamChannel(msg, session, next) { const uid = session.uid; const username = session.get('username'); const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; // 把this.app.get('channelService') 替换成 this.channelService const channel = this.app.get('channelService').getChannel(channelId, false); if (channel) { channel.leave(uid, session.get('sid')); } next(null, { code: 200 }); channel.pushMessage({ route: 'onLeave', user: uid, }) } // 世界聊天 async worldChat(msg, session, next) { const username = session.get('username'); const content = msg.content; const channelId = 'worldChannel'; const channel = this.app.get('channelService').getChannel(channelId, true); channel.pushMessage({ route: 'onChat', from: username, content: content, type: 'world' }); next(null, { code: 200 }); } // 队伍聊天 async teamChat(msg, session, next) { const uid = session.uid; const username = session.get('username'); const content = msg.content; const teamId = msg.teamId; const channelId = `teamChannel_${teamId}`; const channel = this.app.get('channelService').getChannel(channelId, true); if (channel.getMember(uid)) { channel.pushMessage({ route: 'onChat', from: username, content: content, type: 'team', teamId: teamId }); next(null, { code: 200 }); } next(null, { code: Code.CHAT.FA_USER_NOT_TEAM_MEMBER, msg: "User not team member"}); } }