一 生命周期回调
生命周期回调可以让开发人员在不同类型的server生命周期中进行详细操作。
提供的生命周期回调函数包含:beforeStartup,afterStartup,beforeShutdown,afterStartAll。
其详细的功能说明例如以下:
beforeStartup(app, cb) before application start components callback Arguments app - application object cb - callback function afterStartup(app, cb) after application start components callback Arguments app - application object cb - callback function beforeShutdown(app, cb) before application stop components callback Arguments app - application object cb - callback function afterStartAll(app) after all applications started callback Arguments app - application object
详细用法:在game-server/app/servers/某一类型服务器/ 文件夹下加入lifecycle.js文件,详细文件内容例如以下:
module.exports.beforeStartup = function(app, cb) { // do some operations before application start up cb(); }; module.exports.afterStartup = function(app, cb) { // do some operations after application start up cb(); }; module.exports.beforeShutdown = function(app, cb) { // do some operations before application shutdown down cb(); }; module.exports.afterStartAll = function(app) { // do some operations after all applications start up };
二 组件
首先。在app/components/加入HelloWorld.js文件, 大致代码例如以下:
// components/HelloWorld.js module.exports = function(app, opts) { return new HelloWorld(app, opts); }; var HelloWorld = function(app, opts) { this.app = app; this.interval = opts.interval | DEFAULT_INTERVAL; }; HelloWorld.name = ‘__HelloWorld__‘; HelloWorld.prototype.start = function(cb) { console.log(‘Hello World Start‘); cb(); } HelloWorld.prototype.afterStart = function (cb) { console.log(‘Hello World afterStart‘); cb(); } HelloWorld.prototype.stop = function(force, cb) { console.log(‘Hello World stop‘); cb(); }
这样一个组件就创建完毕了。然后在app中配置。在masterserver中载入:
var helloWorld = require(‘./app/components/HelloWorld‘); app.configure(‘production|development‘, ‘master‘, function() { app.load(helloWorld, {interval: 5000}); });
三 演示样例
我这边是在arena服务中增加了lifecycle,同一时候加了一个组件,步骤例如以下。
1 启动过程
2 关闭过程
时间: 2024-10-14 02:56:22