根据 chat 示例创建 game-server,支持分布式部署、域名访问、数据库连接和基础使用
This commit is contained in:
63
game-server/app/servers/connector/handler/entryHandler.ts
Normal file
63
game-server/app/servers/connector/handler/entryHandler.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import {Application} from 'pinus';
|
||||
import {FrontendSession} from 'pinus';
|
||||
|
||||
export default function (app: Application) {
|
||||
return new EntryHandler(app);
|
||||
}
|
||||
|
||||
export class EntryHandler {
|
||||
constructor(private app: Application) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* New client entry chat server.
|
||||
*
|
||||
* @param {Object} msg request message
|
||||
* @param {Object} session current session object
|
||||
*/
|
||||
async enter(msg: { rid: string, username: string }, session: FrontendSession) {
|
||||
let self = this;
|
||||
let rid = msg.rid;
|
||||
let uid = msg.username + '*' + rid;
|
||||
let sessionService = self.app.get('sessionService');
|
||||
|
||||
// duplicate log in
|
||||
if (!!sessionService.getByUid(uid)) {
|
||||
return {
|
||||
code: 500,
|
||||
error: true
|
||||
};
|
||||
}
|
||||
|
||||
await session.abind(uid);
|
||||
session.set('rid', rid);
|
||||
session.push('rid', function (err) {
|
||||
if (err) {
|
||||
console.error('set rid for session service failed! error is : %j', err.stack);
|
||||
}
|
||||
});
|
||||
session.on('closed', this.onUserLeave.bind(this));
|
||||
|
||||
// put user into channel
|
||||
let users = await self.app.rpc.chat.chatRemote.add.route(session)(uid, self.app.get('serverId'), rid, true);
|
||||
|
||||
return {
|
||||
users: users
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* User log out handler
|
||||
*
|
||||
* @param {Object} app current application
|
||||
* @param {Object} session current session object
|
||||
*
|
||||
*/
|
||||
onUserLeave(session: FrontendSession) {
|
||||
if (!session || !session.uid) {
|
||||
return;
|
||||
}
|
||||
this.app.rpc.chat.chatRemote.kick.route(session, true)(session.uid, this.app.get('serverId'), session.get('rid'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user