Application()

Application prototype.

new Application()

Members

static, constant STATE_INITED

app has inited

static, constant STATE_START

app start

static, constant STATE_STARTED

app has started

static, constant STATE_STOPED

app app has stoped

backendSessionService :BackendSessionService

The BackendSessionService

channelService :ChannelService

The ChannelService

components :DefaultComponents

curServer :ServerInfo

current server info

mode :string

App run mode

serverId :string

current server id

serverType :string

current server type

sessionService :SessionService

The SessionService

startTime :number

current server start time, unix timestamp

Methods

static rpcInvoke(serId, msg, cb)

rpc Invoke
Parameters:
Name Type Description
serId String Server'sID
msg Object message object
Properties
Name Type Description
namespace string 'user'|'sys'
serverType string
service string
method string
args Array
cb function callback
Example
app.rpcInvoke( serId,
      { namespace:'user',
        serverType:serType,
        service:'gameRemote',
        method:'leaveGame',
        args: args
      },function( err, result){
          console.log('-----RPCNotify: err: ',err," result: ", result);
      });

addCrons(crons)

Add crons from current application at runtime.
Parameters:
Name Type Description
crons Array new crons would be added in application

addServers(servers)

Add new server info to current application in runtime.
Parameters:
Name Type Description
servers Array new server info list

after(af)

Add after filter.
Parameters:
Name Type Description
af Object | function after filter, `af(err, msg, session, resp, next)`

afterStart(cb) → {Void}

Lifecycle callback for after start.
Parameters:
Name Type Description
cb function callback function
Returns:
Void

before(bf)

Add before filter.
Parameters:
Name Type Description
bf Object | function before filter, bf(msg, session, next)

beforeStopHook(fun) → {Void}

Set before stop function. It would perform before servers stop.
Parameters:
Name Type Description
fun function before close function
Returns:
Void

configure(env, fn, type) → {Application}

Configure callback for the specified env and server type. When no env is specified that callback will be invoked for all environments and when no type is specified that callback will be invoked for all server types. Examples: app.configure(function(){ // executed for all envs and server types }); app.configure('development', function(){ // executed development env }); app.configure('development', 'connector', function(){ // executed for development env and connector server type }); app.configure('development', '!connector|gate', function(){ // executed for development env and not connector,not gate server type });
Parameters:
Name Type Description
env String application environment
fn function callback function
type String server type, If the string starts with ! At first, this configuration is used by all servers except type
Returns:
Application - for chaining

configureLogger(logger)

Configure logger with {$base}/config/log4js.json
Parameters:
Name Type Description
logger Object sex-pomelo-logger instance without configuration

disable(setting) → {app}

Disable `setting`.
Parameters:
Name Type Description
setting String application setting
Returns:
app - for chaining

disabled(setting) → {Boolean}

Check if `setting` is disabled.
Parameters:
Name Type Description
setting String application setting
Returns:
Boolean

enable(setting) → {app}

Enable `setting`.
Parameters:
Name Type Description
setting String application setting
Returns:
app - for chaining

enabled(setting) → {Boolean}

Check if `setting` is enabled.
Parameters:
Name Type Description
setting String application setting
Returns:
Boolean

filter(filter)

add a filter to before and after filter
Parameters:
Name Type Description
filter Object provide before and after filter method. A filter should have two methods: before and after.

get(setting) → {string|ChannelService|SessionService|BackendSessionService}

Get property from setting
Parameters:
Name Type Description
setting string application setting - 'channelService' return @see ChannelService - 'sessionService' return @see SessionService - 'backendSessionService' return @see BackendSessionService - '__routes__' return Object

getBackendSessionService() → {BackendSessionService}

Get BackendSessionService

getBase() → {String}

Get application base path
Returns:
String - application base path
Example
cwd: /home/game/
 pomelo start
 app.getBase() -> /home/game

getCfgPath(cfgFilenullable) → {String}

Get application config path
Parameters:
Name Type Attributes Description
cfgFile String <nullable>
config file name
Returns:
String - application base path
Example
cwd: /home/game/
 pomelo start
 app.getCfgPath() -> /home/game/config/[env]

getCfgSync(cfgFilenullable, parseJsonnullable) → {String}

Get application config file content
Parameters:
Name Type Attributes Default Description
cfgFile String <nullable>
config file name
parseJson boolean <nullable>
false weather parse as JSON, default false
Returns:
String - cfg file content
Example
cwd: /home/game/
 pomelo start
 app.getCfgSync() -> /home/game/config/[env]

getChannelService() → {ChannelService}

Get ChannelService
Returns:
ChannelService

getCurServer() → {ServerInfo}

Get current server info.
Returns:
ServerInfo - current server info, {id, serverType, host, port}

getMaster() → {Object}

Get master server info.
Returns:
Object - master server info, {id, host, port}

getRoutes() → {object}

Get Routes
Returns:
object

getServerById(serverId) → {Object}

Get server info by server id from current server cluster.
Parameters:
Name Type Description
serverId String server id
Returns:
Object - server info or undefined

getServerFromConfig(serverId) → {Object}

Get server info by server id from servers.json.
Parameters:
Name Type Description
serverId String server id
Returns:
Object - server info or undefined

getServerId() → {String|Number}

Get current server id.
Returns:
String | Number - current server id from servers.json

getServers() → {Object}

Get all the current server infos.
Returns:
Object - server info map, key: server id, value: server info

getServersByType(serverType) → {Array}

Get server infos by server type.
Parameters:
Name Type Description
serverType String server type
Returns:
Array - server info list

getServersFromConfig() → {Object}

Get all server infos from servers.json.
Returns:
Object - server info map, key: server id, value: server info

getServerType() → {String|Number}

Get current server type.
Returns:
String | Number - current server type from servers.json

getServerTypes() → {Array}

Get all the server type.
Returns:
Array - server type list

getSessionService() → {SessionService}

Get SessionService
Returns:
SessionService

globalAfter(af)

Add global after filter.
Parameters:
Name Type Description
af Object | function after filter, `af(err, msg, session, resp, next)`

globalBefore(bf)

Add global before filter.
Parameters:
Name Type Description
bf Object | function before filter, bf(msg, session, next)

globalFilter(filter)

add a global filter to before and after global filter
Parameters:
Name Type Description
filter Object provide before and after filter method. A filter should have two methods: before and after.

init()

Initialize the server. - setup default configuration

isBackend(server) → {Boolean}

Check the server whether is a backend server
Parameters:
Name Type Description
server server server info. it would check current server if server not specified
Returns:
Boolean

isFrontend(server) → {Boolean}

Check the server whether is a frontend server
Parameters:
Name Type Description
server server server info. it would check current server if server not specified
Returns:
Boolean

isMaster() → {Boolean}

Check whether current server is a master server
Returns:
Boolean

load(nameopt, component, optsopt) → {Object}

Load component
Parameters:
Name Type Attributes Description
name String <optional>
(optional) name of the component
component Object component instance or factory function of the component
opts Object <optional>
(optional) construct parameters for the factory function
Returns:
Object - app instance for chain invoke

loadConfig(key, val) → {Server|Mixed}

Load Configure json file to settings.
Parameters:
Name Type Description
key String environment key
val String | object environment value
Returns:
Server | Mixed - for chaining, or the setting value

loadConfigBaseApp(key, val, reload) → {Server|Mixed}

Load Configure json file to settings.(support different environment directory & compatible for old path)
Parameters:
Name Type Description
key String environment key
val String environment value
reload Boolean whether reload after change default false
Returns:
Server | Mixed - for chaining, or the setting value

registerAdmin(module, module, opts)

Register admin modules. Admin modules is the extends point of the monitor system.
Parameters:
Name Type Description
module String (optional) module id or provoided by module.moduleId
module Object module object or factory function for module
opts Object construct parameter for module

removeCrons(crons)

Remove crons from current application at runtime.
Parameters:
Name Type Description
crons Array old crons would be removed in application

removeServers(ids)

Remove server info from current application at runtime.
Parameters:
Name Type Description
ids Array server id list

replaceServers(server)

Replace server info from current application at runtime.
Parameters:
Name Type Description
server Object id map

require(relative)

Override require method in application
Parameters:
Name Type Description
relative String path of file

route(serverType, routeFunc) → {Object}

Set the route function for the specified server type. Examples: app.route('area', routeFunc); let routeFunc = function(session, msg, app, cb) { // all request to area would be route to the first area server let areas = app.getServersByType('area'); cb(null, areas[0].id); };
Parameters:
Name Type Description
serverType String server type string
routeFunc function route function. routeFunc(session, msg, app, cb)
Returns:
Object - current application instance for chain invoking

rpcAfter(af)

Add rpc after filter.
Parameters:
Name Type Description
af Object | function after filter, `af(serverId, msg, opts, next)`

rpcBefore(bf)

Add rpc before filter.
Parameters:
Name Type Description
bf Object | function before fileter, bf(serverId, msg, opts, next)

rpcFilter(filter)

add a rpc filter to before and after rpc filter
Parameters:
Name Type Description
filter Object provide before and after filter method. A filter should have two methods: before and after.

set(setting, val, attach) → {Server|Mixed}

Assign `setting` to `val`, or return `setting`'s value. Example: app.set('key1', 'value1'); app.get('key1'); // 'value1' app.key1; // undefined app.set('key2', 'value2', true); app.get('key2'); // 'value2' app.key2; // 'value2'
Parameters:
Name Type Description
setting String the setting of application
val String the setting's value
attach Boolean whether attach the settings to application
Returns:
Server | Mixed - for chaining, or the setting value

start(cb)

Start application. It would load the default components and start all the loaded components.
Parameters:
Name Type Description
cb function callback function

stop(force)

Stop components.
Parameters:
Name Type Description
force Boolean whether stop the app immediately

tr(msg, …paras) → {string}

tr by default lang
Parameters:
Name Type Attributes Description
msg string
paras any <repeatable>
Returns:
string

tr1(locale, msg, …paras) → {string}

tr by lang
Parameters:
Name Type Attributes Description
locale string the locale
msg string
paras any <repeatable>
Returns:
string

transaction(name, conditions, handlers, retry)

Application transaction. Transaction includes conditions and handlers, if conditions are satisfied, handlers would be executed. And you can set retry times to execute handlers. The transaction log is in file logs/transaction.log.
Parameters:
Name Type Description
name String transaction name
conditions Object functions which are called before transaction
handlers Object functions which are called during transaction
retry Number retry times to execute handlers if conditions are successfully executed

use(plugin, optsopt)

Use plugin.
Parameters:
Name Type Attributes Description
plugin Object plugin instance
opts Object <optional>
(optional) construct parameters for the factory function