标签:
项目接近尾声,这个周在弄运维相关的一些东西。var genRouteFun = function() { return function(session, msg, app, cb) { var routes = app.get('__routes__'); if (!routes) { defaultRoute(session, msg, app, cb); return; } var type = msg.serverType, route = routes[type] || routes['default']; if (route) { route(session, msg, app, cb); } else { defaultRoute(session, msg, app, cb); } }; }; var defaultRoute = function(session, msg, app, cb) { var list = app.getServersByType(msg.serverType); if (!list || !list.length) { cb(new Error('can not find server info for type:' + msg.serverType)); return; } var uid = session ? (session.uid || '') : ''; var index = Math.abs(crc.crc32(uid.toString())) % list.length; utils.invokeCallback(cb, null, list[index].id); };
module.exports = function(app, opts) { return new HttpServer(app, opts); }; var HttpServer = function(app, opts) { this.app = app; this.opts = opts; this.confs = {}; // 指令对应的函数 var confs = { data: this.onData }; this.server = http.createServer(function(req, res){ var post = ''; req.on('data', function(chunk) { post += chunk; }); req.on('end', function() { // 从post消息中取得指令 post = querystring.parse(post); var cmd = post.cmd; if (!!cmd) { var func = confs[cmd]; if (!!func) { func(post.uid, function(ret){ res.write(JSON.stringify(ret)); res.end(); }); } else { } } }); }); }; HttpServer.name = '__HttpServer__'; HttpServer.prototype.start = function(cb) { this.server.listen(this.opts.port); cb(); }; HttpServer.prototype.afterStart = function(cb) { cb(); }; HttpServer.prototype.stop = function(force, cb) { this.server.close(); cb(); }; HttpServer.prototype.onData = function(uid, cb) { var servers = pomelo.app.getServersByType('area'); var server = dispatcher.dispatch(uid, servers); var sid = server.id; pomelo.app.rpc.area.httpRemote.getUser.toServer(sid, uid, function(res){ cb(res); }); };
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/xufeng0991/article/details/47784101