根据 chat 示例创建 game-server,支持分布式部署、域名访问、数据库连接和基础使用

This commit is contained in:
liangtongchuan
2020-08-15 20:34:31 +08:00
parent e52a829567
commit 8ce0dc040f
36 changed files with 3165 additions and 2 deletions

View 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
}]);
}
}
}

View File

@@ -0,0 +1,92 @@
import { Application, ChannelService, FrontendSession, RemoterClass } from 'pinus';
export default function (app: Application) {
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');
}
private channelService: ChannelService;
/**
* Add user into chat channel.
*
* @param {String} uid unique id for user
* @param {String} sid server id
* @param {String} name channel name
* @param {boolean} flag channel parameter
*
*/
public async add(uid: string, sid: string, name: string, flag: boolean) {
let channel = this.channelService.getChannel(name, flag);
let username = uid.split('*')[0];
let param = {
user: username
};
channel.pushMessage('onAdd', param);
if (!!channel) {
channel.add(uid, sid);
}
return this.get(name, flag);
}
/**
* Get user from chat channel.
*
* @param {Object} opts parameters for request
* @param {String} name channel name
* @param {boolean} flag channel parameter
* @return {Array} users uids in channel
*
*/
private get(name: string, flag: boolean) {
let users: string[] = [];
let channel = this.channelService.getChannel(name, flag);
if (!!channel) {
users = channel.getMembers();
}
for (let i = 0; i < users.length; i++) {
users[i] = users[i].split('*')[0];
}
return users;
}
/**
* Kick user out chat channel.
*
* @param {String} uid unique id for user
* @param {String} sid server id
* @param {String} name channel name
*
*/
public async kick(uid: string, sid: string, name: string) {
let channel = this.channelService.getChannel(name, false);
// leave channel
if (!!channel) {
channel.leave(uid, sid);
}
let username = uid.split('*')[0];
let param = {
user: username
};
channel.pushMessage('onLeave', param);
}
}