r2/game-server/app.js
lightings 0b35eb3e7e ...
2023-05-19 11:38:32 +08:00

85 lines
2.0 KiB
JavaScript

var pomelo = require('pomelo');
const globalChannel = require('pomelo-globalchannel-plugin');
const dispatcher = require('./app/util/dispatcher');
const ChatService = require('./app/services/chatService');
const redis = require("redis");
/**
* Init app for client.
*/
var app = pomelo.createApp();
app.set('name', 'r2');
app.loadConfig('mysql', app.getBase() + '/config/mysql.json');
app.loadConfig('redis', app.getBase() + '/config/redis.json');
app.loadConfig('gameapi', app.getBase() + '/config/gameapi.json');
const redisConfig = pomelo.app.get("redis");
app.use(globalChannel, { globalChannel: {
host: redisConfig.host,
port: redisConfig.port,
db: redisConfig.db,
}});
var chatRoute = function(session, msg, app, cb) {
var chatServers = app.getServersByType('chat');
if(!chatServers || chatServers.length === 0) {
cb(new Error('can not find chat servers.'));
return;
}
var res = dispatcher.dispatch(session.get('uid'), chatServers);
cb(null, res.id);
};
// app configuration
app.configure('production|development', function(){
let serverConfig = {
'reloadHandlers': true,
'reloadRemotes': true,
};
app.set('serverConfig', serverConfig);
app.set('connectorConfig',
{
connector : pomelo.connectors.hybridconnector,
heartbeat : 3,
useDict : true,
useProtobuf : true
});
app.enable('systemMonitor');
app.route('chat', chatRoute)
});
app.configure('production|development', 'chat', function(){
app.set('chatService', new ChatService(app));
});
app.configure('production|development', 'master', function() {
const config = redisConfig;
// cleanup redis
const client = redis.createClient({
url: `redis://${config.host}:${config.port}/${config.db}`,
});
client.on("error", (err) => {
console.error(err);
});
async function init() {
await client.connect();
await client.flushDb();
client.disconnect();
}
init();
});
// start app
app.start();
process.on('uncaughtException', function (err) {
console.error(' Caught exception: ' + err.stack);
});