The beginning of a single step, has been said to want to understand pomelo, pomelo interested, but has been slow to touch, although the pomelo for the source analysis, in the network certainly not only I, has a very good predecessors to walk in the front, such as http://golanger.cn/, In reading Pomelo code, has been serialized to 11, in my source analysis reference to the blog, of course, will also add my understanding of pomelo, so hope to improve their understanding of node.js and learn some excellent design.
Development environment: Win7 Debugging Environment: webstorm5.0 node.js Version: v0.8.21 source version Package.json:
{
"name": "Chatofpomelo", "
Version": "0.0.1",
"private": false,
"dependencies": {
"pomelo": " 0.2.0 ",
" Log4js ":" >= 0.4.1 ",
" CRC ":" >=0.0.1 "
}
Gameserver/app.js
var pomelo = require (' pomelo ');
var routeutil = require ('./app/util/routeutil ');
/**
* Init app for client.
* *
var app = Pomelo.createapp (); Create Application
app.set (' name ', ' Chatofpomelo '); Set application name
//app Configure
app.configure (' production|development ', function () {
//route Configures
app.route (' chat ', routeutil.chat);
Filter Configures
App.filter (Pomelo.timeout ());
});
Start App
App.start ();
Process.on (' Uncaughtexception ', function (err) {
console.error (' Caught exception: ' + Err.stack);
});
Note: Debugging under Webstorm may cause the application's execution path problems because of the setting of the working directory, resulting in the inability to read the configuration file, so you need to modify the following according to the actual situation
var opt = {' base ': ' D:\\src\\pomelo\\chatofpomelo\\game-server '}
var app = Pomelo.createapp (opt);
App.set (' name ', ' Chatofpomelo ');
Opt.base is the actual directory path for your game-server, which can be tailored to your needs.
App.js is the main portal for Game-server, primarily responsible for creating application, reading configuration files, applying to application settings, and using App.start () to perform the actual master, The start of servers such as monitor, for chat room programs, but also to do simple routing and filtering settings.
Application, the application of the definition, component management, context configuration, which makes the pomelo framework of the external interface is very simple, and has a loose coupling, pluggable architecture.
All servers are started from the run App.js. Each server's startup first creates a globally unique application object that mounts all the information about the server, including server physical information, server logic information, and pomelo component information. The object also provides basic methods such as application management and configuration. When the App.start () method is invoked in App.js, the Application object first loads the default component through the Loaddefaultcomponents method.
Pomelo/lib/pomelo.js
var application = require ('./application ');
Pomelo.createapp = function (opts) {
var app = Application;
App.init (opts);
Self.app = app;
return app;
Pomelo/lib/application.js
/**
* Application prototype.
*
* @module
/var application = Module.exports = {};
/**
* Application States * *
var state_inited = 1; App has inited
var state_start = 2; App start
var state_started = 3; App has started
var state_stoped = 4; App has stoped
/**
* Initialize the server.
* -Setup default configuration
* *
@api Private *
/application.init = function (opts) {
opts = opts | | {};
Logger.info (' app.init invoked ');
this.loaded = [];
This.components = {};
This.settings = {}; Set, and get function of the container
this.set (' base ', opts.base); Set up the working directory of the Server
this.defaultconfiguration (); According to the configuration file, load master,monitor server
this.state = state_inited; Application working status
logger.info (' Application inited:%j ', This.get (' ServerID '));
Pomelo/lib/application.js
/**
* Initialize application configuration.
*
@api Private *
*
application.defaultconfiguration = function () {
var args = Utils.argsinfo ( PROCESS.ARGV);
This.setupenv (args); Application Environment Setting
this.loadservers (); Load server configuration information
this.loadconfig (' Master ', this.getbase () + '/config/master.json ');//Load Mater server configuration information
This.processargs (args); Set the server configuration This.configlogger () according to the startup parameters
;
Utils.argsinfo (PROCESS.ARGV); To get the system startup parameters, we might as well see what parameters are supported
Start Game-server server: >pomelo start [Development | production] [--daemon]
According to the args parameter, the application working environment is development or production
/**
* Setup enviroment.
* @api Private
*/
application.setupenv = function (args) {
this.set (' env ', args.env | | process.env.NODE_ ENV | | ' Development ', true);
Load server information and save in __servermap___ memory
/**
* Load Server info from configure file.
*
@api Private *
*
application.loadservers = function () {
this.loadconfig (' Servers ', This.getbase () + '/config/servers.json ');
var servers = this.get (' servers ');
var servermap = {}, slist, I, L, server;
for (var servertype in servers) {
slist = Servers[servertype];
For (i=0, l=slist.length; i<l; i++) {
server = Slist[i];
Server.servertype = ServerType;
Servermap[server.id] = server;
}
}
This.set (' __servermap__ ', servermap);
Server.json
{"Development": {"connector": [{"id": "connector-server-1", "host": "127.0.0.1", "Port": 4050, "WsP Ort ": 3050}, {" id ":" connector-server-2 "," host ":" 127.0.0.1 "," Port ": 4051," Wsport ": 3051}, {" id ":" Connector-server-3 "," host ":" 127.0.0.1 "," Port ": 4052," Wsport ": 3052}]," chat ": [{" id ":" Chat
-server-1 "," host ":" 127.0.0.1 "," Port ": 6050}, {" id ":" chat-server-2 "," host ":" 127.0.0.1 "," Port ": 6051}, {"id": "chat-server-3", "host": "127.0.0.1", "Port": 6052}], "gate": [{"id": "gate-server-1", " Host ":" 127.0.0.1 "," Wsport ": 3014}]}," production ": {" connector ": [{" id ":" connector-server -1 "," host ":" 127.0.0.1 "," Port ": 4050," Wsport ": 3050}, {" id ":" connector-server-2 "," host ":" 127.0.0.1 "," Port ":
4051, "Wsport": 3051}, {"id": "connector-server-3", "host": "127.0.0.1", "Port": 4052, "Wsport": 3052}],
"Chat": [ {"id": "chat-server-1", "host": "127.0.0.1", "Port": 6050}, {"id": "chat-server-2", "host": "127.0.0.1", "p Ort ": 6051}, {" id ":" chat-server-3 "," host ":" 127.0.0.1 "," Port ": 6052}]," gate ": [{" id ": "Gate-server-1", "host": "127.0.0.1", "Wsport": 3014}]}}
Tool function, read the JSON configuration file, where the Master.json file is read
/**
* Load Configure JSON file to settings.
*
* @param {string} Key Environment key
* @param {string} val Environment Value
* @return {server| Mixed} for chaining, or the setting value
*
* @memberOf application
/
application.loadconfig = function (Key, Val) {
var env = this.get (' env ');
val = require (val);
if (Val[env]) {
val = val[env];
}
This.set (Key, Val);
Master.json
{"
development": {"
id": "master-server-1",
"host": "127.0.0.1",
"port": 3005,
"Queryport ": 3015,
" Wsport ": 2337
},
" production ": {
" id ":" master-server-1 ",
" host ":" 127.0.0.1 ",
"Port": 3005,
"Queryport": 3015,
"Wsport": 2337
}
}
Configure the server according to the process to read the configured parameters.
Application.processargs = function (args) {
var servertype = Args.servertype | | ' Master ';
var ServerID = Args.serverid | | This.get (' master '). ID;
This.set (' main ', Args.main, true);
This.set (' ServerType ', servertype, true);
This.set (' ServerID ', ServerID, true);
if (servertype!== ' master ') {
this.set (' Curserver ', This.getserverbyid (ServerID), true);
} else {
This.set (' Curserver ', This.get (' master '), true);
Log configuration of the project
Application.configlogger = function () {
if (Process.env.POMELO_LOGGER!== ' off ') {
log.configure (this, This.getbase () + '/config/log4js.json ');
}
;
Log4js.json
{' Appenders ': [{' type ': ' file ', ' filename ': './logs/node-log-${opts:serverid}.log ', ' fileSize ':
1048576, "layout": {"type": "Basic"}, "Backups": 5}, {"Type": "Console"
}, {"Type": "File", "filename": "./logs/con-log-${opts:serverid}.log", "pattern": "Connector", "FileSize": 1048576, "layout": {"type": "Basic"}, "Backups": 5, "category": "con- Log "}, {" Type ":" File "," filename ":"./logs/rpc-log-${opts:serverid}.log "," fileSize ": 1048576
, "layout": {"type": "Basic"}, "Backups": 5, "category": "Rpc-log"}, {
' type ': ' file ', ' filename ': './logs/forward-log-${opts:serverid}.log ', ' fileSize ': 1048576, ' layout ': { ' Type ': ' Basic '}, ' Backups ': 5, ' category ': ' Forward-log '}, {' type ': ' File ' , "filename": "./LOgs/crash.log "," fileSize ": 1048576," layout ": {" type ":" Basic "}," Backups ": 5, "category": "Crash-log"}], "levels": {"Rpc-log": "Error", "Forward-log": "Error"}, "Replaceco Nsole ": true}