From 5f884bec20f982f4979174e6a667865ba43d9bdc Mon Sep 17 00:00:00 2001 From: liangtongchuan Date: Thu, 2 Sep 2021 18:30:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=20app.ts=20=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=BA=94=E7=94=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game-server/app.ts | 160 ++++++++++++++++++++------------------------- 1 file changed, 71 insertions(+), 89 deletions(-) diff --git a/game-server/app.ts b/game-server/app.ts index 2534a91d0..f8b636419 100644 --- a/game-server/app.ts +++ b/game-server/app.ts @@ -56,9 +56,9 @@ preload(); * Init app for client. */ let app = pinus.createApp(); -app.set('name', 'chatofpomelo-websocket'); +app.set('name', 'zyz'); -// 加载数据库和redis +// 全局配置,对所有 env 所有 serverType 生效 app.configure(function () { app.loadConfig('database', app.getBase() + '/config/database'); app.loadConfig('serverName', app.getBase() + '/config/serverName'); @@ -66,30 +66,33 @@ app.configure(function () { console.log('db:', app.get('database')); console.log('serverName:', app.get('serverName')) - mongoose.connect(app.get('database').mongo, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false }, (err) => { - if (err) { - errlogger.error('mongodb connect err', err); - } else { - console.log('mongodb connect suc'); - } - }); + initMongodb(app); + initRedis(app); + setupRoutes(app); + setupFilters(app); - const redisClient = connectRedis(app.get('database').redis, app.get('database').redispw); - app.set('redis', redisClient); - redLockService.initRedlock(redisClient); - redlockCacheService.init(); - // TODO 重启 1 次只需要初始化 1 次,判断方法可以优化 - if (app.serverId === 'master-server-1') { - redisService.readDataBase(); - redisService.initAllRank(); - redisService.clearComBtlQueue(); - redisService.clearChannelServers(); - updateTeamStatus(COM_TEAM_STATUS.DEFAULT, COM_TEAM_STATUS.LOOSE); - updateTeamStatus(COM_TEAM_STATUS.FIGHTING, COM_TEAM_STATUS.LOOSE); - } + app.set(RESERVED.ERROR_HANDLER, errorHandler); + app.set(RESERVED.GLOBAL_ERROR_HANDLER, globalErrorHandler); + // RPC 启用TCP协议 + app.set('proxyConfig', { + mailboxFactory: createTcpMailBox, + // bufferMsg:true + // rpc 超时时间 + // timeout: 20 * 1000, + // dynamicUserProxy: true, + }); + app.set('remoteConfig', { + acceptorFactory: createTcpAcceptor, + // bufferMsg:true, + // interval:50, + }); + app.set('serverConfig', { + 'reloadHandlers': true, + 'reloadRemotes': true, + }); }); -// app configuration +// 对所有 env 的 个别 serverType 进行配置 app.configure(ALL_ENVS, 'connector', function () { app.set('connectorConfig', { @@ -99,14 +102,6 @@ app.configure(ALL_ENVS, 'connector', function () { useProtobuf: true }); - /** - // 缓存大小不够 日志示例 - [2020-03-27T10:44:48.752] [ERROR] pinus - [chat-server-1 channelService.js] [pushMessage] fail to dispatch msg to serverId: connector-server-1, err:RangeError [ERR_OUT_OF_RANGE]: The value of "offset" is out of range. It must be >= 0 and <= 0. Received 1 - at boundsError (internal/buffer.js:53:9) - at writeU_Int8 (internal/buffer.js:562:5) - at Buffer.writeUInt8 (internal/buffer.js:569:10) - at Encoder.writeBytes (F:\develop\gong4-server\logicServer\pinus\packages\pinus-protobuf\lib\encoder.ts:195:20) - */ app.set('protobufConfig', { // protobuf Encoder 使用 5m 的缓存 需要保证每个消息不会超过指定的缓存大小,超过了就会抛出异常 encoderCacheSize: 5 * 1024 * 1024, @@ -124,28 +119,14 @@ app.configure(ALL_ENVS, 'gate', function () { }); app.configure(ALL_ENVS, 'gm', function () { - app.set('connectorConfig', - { - connector: pinus.connectors.hybridconnector, - useProtobuf: true - }); init();//将gm后台数据加载到gate服 }); app.configure(ALL_ENVS, 'guild', function () { - app.set('connectorConfig', - { - connector: pinus.connectors.hybridconnector, - useProtobuf: true - }); -}); + app.filter(guildAuthFilter(app)); +}) app.configure(ALL_ENVS, 'systimer', function () { - app.set('connectorConfig', - { - connector: pinus.connectors.hybridconnector, - useProtobuf: true - }); timeTaskService.init(); }); @@ -171,10 +152,47 @@ export function globalErrorHandler(err: Error, msg: any, resp: any, } } -// app configure -app.configure(ALL_ENVS, function () { - app.set(RESERVED.ERROR_HANDLER, errorHandler); - app.set(RESERVED.GLOBAL_ERROR_HANDLER, globalErrorHandler); +function initMongodb(app: _pinus.Application) { + mongoose.connect(app.get('database').mongo, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false }, (err) => { + if (err) { + errlogger.error('mongodb connect err', err); + } else { + console.log('mongodb connect suc'); + } + }); +} + +function initRedis(app: _pinus.Application) { + const redisClient = connectRedis(app.get('database').redis, app.get('database').redispw); + app.set('redis', redisClient); + redLockService.initRedlock(redisClient); + redlockCacheService.init(); + // TODO 重启 1 次只需要初始化 1 次,判断方法可以优化 + if (app.serverId === 'master-server-1') { + redisService.readDataBase(); + redisService.initAllRank(); + redisService.clearComBtlQueue(); + redisService.clearChannelServers(); + updateTeamStatus(COM_TEAM_STATUS.DEFAULT, COM_TEAM_STATUS.LOOSE); + updateTeamStatus(COM_TEAM_STATUS.FIGHTING, COM_TEAM_STATUS.LOOSE); + } +} + +function setupRoutes(app: _pinus.Application) { + app.route('chat', routeUtil.chat); + app.route('battle', routeUtil.battle); + app.route('gm', routeUtil.gm); + app.route('guild', routeUtil.guild); + app.route('activity', routeUtil.activity); + app.route('order', routeUtil.order); +} + +function setupFilters(app: _pinus.Application) { + // filter configures + app.filter(new pinus.filters.time()); + app.filter(new pinus.filters.timeout()); + + app.filter(globalFilter(app)); app.globalAfter((err: Error, routeRecord: RouteRecord, msg: any, session: FrontendOrBackendSession, resp: any, cb: HandlerCallback) => { infologger.debug('global after ', msg && msg.route, msg && JSON.stringify(msg.body), session && session.get('roleId')); if (err) infologger.debug(err); @@ -189,46 +207,10 @@ app.configure(ALL_ENVS, function () { } cb(null); }); - - // route configures - app.route('chat', routeUtil.chat); - app.route('battle', routeUtil.battle); - app.route('gm', routeUtil.gm); - app.route('guild', routeUtil.guild); - app.route('activity', routeUtil.activity); - app.route('order', routeUtil.order); - - // filter configures - app.filter(new pinus.filters.time()); - app.filter(new pinus.filters.timeout()); - - app.filter(globalFilter(app)); - - // RPC 启用TCP协议 - app.set('proxyConfig', { - mailboxFactory: createTcpMailBox, - // bufferMsg:true - // rpc 超时时间 - // timeout: 20 * 1000, - // dynamicUserProxy: true, - }); - app.set('remoteConfig', { - acceptorFactory: createTcpAcceptor, - // bufferMsg:true, - // interval:50, - }); - app.set('serverConfig', { - 'reloadHandlers': true, - 'reloadRemotes': true, - }); -}); - -app.configure(ALL_ENVS, 'guild', function () { - app.filter(guildAuthFilter(app)); -}) +} // TODO: 所有环境打开监听模块和日志 -app.configure(ALL_ENVS, function () { +app.configure(function () { // enable the system monitor modules app.enable('systemMonitor'); app.enable('rpcDebugLog');