lightings 6770bb5045 ...
2023-04-19 15:15:04 +08:00

119 lines
3.0 KiB
JavaScript

var logger = require("pomelo-logger").getLogger(__filename);
const userDao = require("../../../dao/userDao");
module.exports = function (app) {
return new Handler(app);
};
var Handler = function (app) {
this.app = app;
this.channelService = this.app.get("channelService");
};
/** @api {post} connector.entryHandler.entry entry 建立连接-登录-入口
*
* @apiGroup connector
*
* @apiParam {String} uid 用户ID
*
* @apiSuccess {Number} code 状态码
* @apiSuccess {String} msg 消息
*
* @apiSuccessExample {json} Success-Response:
* {
* "code": 200,
* "msg": "game server is ok."
* }
*
* @apiErrorExample {json} Error-Response:
* {
* "code": 500
* "error": true
* }
*
*/
Handler.prototype.entry = function (msg, session, next) {
const uid = msg.uid;
const sessionService = this.app.get("sessionService");
const serverId = this.app.serverId;
console.log(`-------entry ${session.id} : ${uid}`);
// 重复登录检查
// if( !! sessionService.getByUid(uid)) {
// console.log(`-------kick ${session.id} : ${uid}`)
// sessionService.kick(uid, ()=>{
// console.log(`-------kicked ${session.id} : ${uid}`)
// bindUser();
// return;
// })
// }
(bindUser.bind(this))();
async function bindUser() {
const user = await userDao.getUserInfo(msg.uid);
console.log(`-------bind ${session.id} : ${uid}`);
session.bind(uid, () => {
session.set("uid", uid);
session.set("userInfo", user);
session.set("sid", serverId);
session.on("closed", onUserLeave.bind(this, this.app));
session.pushAll((err) => {
if (err) {
logger.error("error: %j", err.stack);
next(null, { code: 500, error: true });
return;
}
next(null, { code: 200, msg: "game server is ok." });
});
});
}
};
var onUserLeave = function (app, session, reason) {
if (!session || !session.uid) {
return;
}
console.log(session);
const channelId = "worldChannel";
const channel = this.channelService.getChannel(channelId, false);
if (channel) {
channel.leave(session.uid, session.get("sid"));
}
logger.debug(`onUserLeave ${session.id} : ${session.uid}`);
};
/**
* Publish route for mqtt connector.
*
* @param {Object} msg request message
* @param {Object} session current session object
* @param {Function} next next step callback
* @return {Void}
*/
Handler.prototype.publish = function (msg, session, next) {
var result = {
topic: "publish",
payload: JSON.stringify({ code: 200, msg: "publish message is ok." }),
};
next(null, result);
};
/**
* Subscribe route for mqtt connector.
*
* @param {Object} msg request message
* @param {Object} session current session object
* @param {Function} next next step callback
* @return {Void}
*/
Handler.prototype.subscribe = function (msg, session, next) {
var result = {
topic: "subscribe",
payload: JSON.stringify({ code: 200, msg: "subscribe message is ok." }),
};
next(null, result);
};