pomelo和客户端保持长链接,推送可以根据频道推送或者根据用户连接的服务器推送
let channel = this.app.get('channelService').getChannel(channelName, true);
参数1为频道的名字,参数2为是否在没有该频道的时候创建一个
channel.add(uid, sid);
将用户放到频道里面,其中uid为用户唯一标识,sid为connector的服务器Id
假设服务器配置是
"connector":[
{"id":"c1", "host":"172.16.46.14","port":4050, "clientPort": 3050, "frontend": true,"cpu": 1},
{"id":"c2", "host":"172.16.46.14", "port":4051, "clientPort": 3051, "frontend": true,"cpu": 2},
{"id":"c3", "host":"172.16.46.14", "port":4052, "clientPort": 3052, "frontend": true,"cpu": 3},
{"id":"c4", "host":"172.16.46.14", "port":4053, "clientPort": 3053, "frontend": true,"cpu": 4}
]
并且用户123和connector2连接,用户234连接connector3
channel.add(123, c2);
channel.add(234, c3);
let channel = this.app.get('channelService').getChannel(channelName);
channel.pushMessage(event, msg, cb);
其中event为客户端监听的push方法,例如onAdd
msg为推送参数,随意传
cb为推送后的回调函数
以上方法可以给用户123和用户234推送同一条消息
let uids = [];
uids.push({uid:123,sid:"c2"});
uids.push({uid:234,sid:"c3"});
this.app.get('channelService').pushMessageByUids(method, params, uids);
其中method为客户端监听的push方法,例如onAdd
params为推送参数,随意传
npm install --save bearcat
var bearcat = require('bearcat');
let contextPath = require.resolve('./context.json');
bearcat.createApp([contextPath]);
bearcat.start(function() {
Configure();
app.set('bearcat', bearcat);
// start app
app.start();
});
其中context.json为
{
"name": "myApp",
"scan": "app", //需要扫描app文件夹下的所有js文件
"beans": []
}
var bearcat = require('bearcat');
var Handler = function (app) {
this.app = app;
this.redis = null; //假设要用到redis
};
var handler = Handler.prototype;
handler.queryEntry = function (msg, session, next) {
//自己的业务逻辑
}
module.exports = function (app) {
return bearcat.getBean({
id: "gateHandler",
func: Handler,
args: [{
name: "app",
value: app
}],
props: [
{name: "redis", ref: "redis"}
]
});
};
其他地方需要使用gateHandler的时候,只需要
var other= function () {
this.gateHandler=null;
}
other.prototype.funA= function(){
this.gateHandler.queryEntry();
...
}
module.exports = {
id: "other",
func: other,
props: [
{name: "gateHandler", ref: "gateHandler"}
]
}
npm install --save bearcat
var bearcat = require('bearcat');
let contextPath = require.resolve('./context.json');
bearcat.createApp([contextPath]);
bearcat.start(function() {
Configure();
app.set('bearcat', bearcat);
// start app
app.start();
});
app.configure('development', 'chat', function() { //chat类型的服务器在development环境下的配置
let chat_OnLineService = bearcat.getBean('chat_OnLineService');
chat_OnLineService.init(app);
});
其中context.json为
{
"name": "myApp",
"scan": "app", //需要扫描app文件夹下的所有js文件
"beans": []
}
var OnLineService = function() {};
OnLineService.prototype.init=function (app) {
this.app = app;
}
module.exports = {
id: "chat_OnLineService",
func: OnLineService,
props: []
}
注意onlineService.js必须在app文件夹下,不然扫描不到
其余使用方式handler、remote和普通js文件一模一样
pomelo进行远程调用时(1、系统远程调用,一般调用Handler,例如客户端调用pomelo的online.onLineHandler.showOnlinePlayers方法;2、用户远程调用,一般调用Remote,例如app.rpc.chat.onLineRemote.kick,是由用户在服务端代码中直接调用),有个默认路由规则
var uid = session ? (session.uid || '') : '';
var index = Math.abs(crc.crc32(uid.toString())) % list.length;
utils.invokeCallback(cb, null, list[index].id);
即先判断有无session参数传入,若有则使用session中绑定好的uid否则用空串,然后进行路由。
当用户未进行session.bind(uid)之前进行远程调用的时候,如果用户没有手动配置路由规则,即使有若干同类型的服务器,也只会调用其中的一台,负载均衡的效果就没有了。
所以,如果要在未绑定uid的时候进行多端口或者多服务器的负载均衡,必须要自定义路由规则而不使用默认规则(在进行auth鉴权服务器调用的时候,往往还不知道用户的Id)
exp.auth = function(session, msg, app, cb) {
let chatServers = app.getServersByType('auth');
if(!chatServers || chatServers.length === 0) {
cb(new Error('can not find cron servers.'));
return;
}
cb(null, chatServers[session.id%chatServers.length].id); //推荐使用session的id或者用户的设备信息等进行路由到不同的服务器或者端口
};
app.route('auth', routeUtil.auth);
{
"type": "dateFile",
"filename": "${opts:base}/logs/cron-${opts:serverId}.log",
"compress": true,
"pattern": ".yyyy-MM-dd",
"layout": {
"type": "basic"
}
,"backups": 0,
"category":"cron"
}
let logger = require('pomelo-logger').getLogger('cron', __filename);
logger.error("***")
{
"type": "dateFile",
"filename": "${opts:base}/logs/service-${opts:serverId}.log",
"layout": {
"type": "basic"
},
"pattern": ".yyyy-MM-dd-hh",
"compress": true,
"backups": 0,
"category":"service"
}
let logger = require('pomelo-logger').getLogger('service', __filename);
logger.error("***")
{
"type": "dateFile",
"filename": "${opts:base}/logs/error.log",
"compress": true,
"pattern": ".yyyy-MM-dd",
"layout": {
"type": "basic"
}
,"backups": 0,
"category":"error"
}
let logger = require('pomelo-logger').getLogger('error', __filename);
logger.error("***")
pomelo start -D
pomelo list
/usr/local/bin/node /data/node/root/app.js env=dev type=all
/usr/local/bin/node /data/node/root/app.js env=dev id=c1 host=172.*.*.* port=4050 clientPort=7003 frontend=true serverType=connector
/usr/local/bin/node /data/node/root/app.js env=dev id=ch4 host=172.*.*.* port=6053 serverType=chat
先写到这吧··有问题联系 滕庆亚