/* * web端請求pomelo服務器 */?
/*?
pomelo服務器只有一個master服務器,master服務器代理masterAgent代為管理master的所有進程.?
一個服務器監聽多個進程,這些進程都會在agent的管理下.?
pomelo 自身有socket.io模塊,web請求pomelo 服務器就采用socket.io 不是websocket連接.?
pomelo-admin模塊下 masterAgent.js文件 有一個listen會監聽master服務器的所有事件. 這里主要是用到?
socket.on('client',function( msg ){ .... }); 監聽客戶端發來的請求?
msg參數是客戶端呢發來的信息 判斷msg中是否有命令?
我 這里的參數是沒有命令的?
execute方法 該方法會調用moduleId下的clientHandler方法 這個方法里才是真正處理業務邏輯的地方?
?self.consoleService.execute(msg.moduleId, 'clientHandler', msg.body,function(err, res) {?
? ? ? ?if (protocol.isRequest(msg)) {?
? ? ? ? ? ?var resp = protocol.composeResponse(msg, err, res);?
? ? ? ? ? ?if (resp) {?
? ? ? ? ? ? ? ? ?socket.emit('client', resp);
? ? ? ? ? ?}?
? ? ? ?} else { //notify should not have a callback
? ? ? ? ? ?logger.warn('notify should not have a callback.');
? ? ? }
?});?
?參數中有一個moduleId 這個參數是在app.js文件中注冊生成的, ======================================================== app.configure('production|development' ,function () {?
?var module = require("./modules/module.js");?
app.registerAdmin(module, {app: app});?
========================================================
======================================================== //module.js?
module.exports = function (opts) {?
?return new Module(opts);?
};
//對應consoleService.execute 方法中的msg.moduleId
?module.exports.moduleId = 'module';?
?var Module = function (opts) {
? ? ? ? opts = opts || {};?
? ? ? ? this.app = opts.app;?
};
?Module.prototype.clientHandler = function (agent, msg, cb) {?
? ? ? ? ? //業務邏輯都寫在這里面 cb 將結果返回?
?};
========================================================?