根据 chat 示例创建 game-server,支持分布式部署、域名访问、数据库连接和基础使用
This commit is contained in:
76
game-server/app/servers/chat/handler/chatHandler.ts
Normal file
76
game-server/app/servers/chat/handler/chatHandler.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { UserModel } from './../../../db/User';
|
||||
import { ChatRemote } from '../remote/chatRemote';
|
||||
import {Application, BackendSession} from 'pinus';
|
||||
import { FrontendSession } from 'pinus';
|
||||
|
||||
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);
|
||||
|
||||
console.log(`got user in send2 :`,);
|
||||
const user = await UserModel.findOneAndUpdate({userName: username}, {userName: username, userNo: 666}, {upsert: true, new: true}).lean();
|
||||
console.log(`got user in send2 :`, user);
|
||||
|
||||
// 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
|
||||
}]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user