var logger = require("pomelo-logger").getLogger(__filename); const userDao = require("../../../dao/userDao"); const Code = require("shared/code"); const axios = require("axios"); const valid_entry_login = module.exports = function (app) { return new Handler(app); }; var Handler = function (app) { this.app = app; this.gameapiUrl = this.app.get("gameapi").gameBaseURL; this.valid_entry_login = axios.create({baseURL:this.gameapiUrl}); this.channelService = this.app.get("channelService"); this.gcs = this.app.get("globalChannelService"); }; /** @api {post} connector.entryHandler.entry entry 建立连接-登录-入口 * * @apiGroup connector * * @apiParam {String} uid 用户ID * @apiParam {String} sid Game Session 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}`); // 重复登录检查 const oldSession = sessionService.getByUid(uid); if (oldSession) { oldSession.forEach(element => { sessionService.kick(element.uid, (()=>{ console.log(`-------kicked ${element.id} : ${element.uid}`); (bindUser.bind(this))(); }).bind(this)); }); } else { (bindUser.bind(this))(); } async function bindUser() { const user = await userDao.getUserInfo(msg.uid); console.log(`-------bind ${session.id} : ${uid}`); // const userInfo = await this.valid_entry_login.get(`&target_id=${uid}`); // if (userInfo.code == 200) { // } else { // next(null, { code: 500, msg: "login status invalid" }); // return; // } 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; } const channelId = `privateChannel_${uid}`; this.gcs.add(channelId, session.uid, session.get("sid"), function () {}); next(null, { code: 200, msg: "game server is ok." }); }); }); } }; /** @api {post} connector.entryHandler.heartbeat heartbeat 心跳 * @apiGroup connector * * @apiSuccessExample {json} Success-Response: * { * } * */ Handler.prototype.heartbeat = function (msg, session, next) { next(null, {}); } var onUserLeave = function (app, session, reason) { if (!session || !session.uid) { return; } logger.debug(`onUserLeave ${session.id} : ${session.uid}`); const uid = session.uid; const sid = session.get("sid"); const privateChannelId = `privateChannel_${uid}`; this.gcs.leave(privateChannelId, uid, sid); const teamid = session.get("teamid"); app.rpc.chat.chatRemote.kick(session, uid, sid, teamid, null); }; /** * 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); };