pomelo学习笔记

路由

比如有多个聊天服务器,我们需要把消息路由的不同的服务器下处理

/*
msg结构:
{
  namespace: 'sys',
  serverType: 'gate',
  service: 'msgRemote',
  method: 'forwardMessage',
  args: [
    {
      id: 1,
      type: 0,
      compressRoute: 0,
      route: 'gate.gateHandler.getServer',
      body: [Object],
      compressGzip: 0
    },
    {
      id: 1,
      frontendId: 'connector-server-1',
      uid: null,
      settings: {}
    }
  ]
}
*/
function myRoute(session, msg, app, cb) {
    var chatServers = app.getServersByType('chat'); 
    if (!chatServers) {
     	cb(new Error('can not find chat servers.'));
		return;
    }
    cb(null, chatServers[0].id);//把所有的发往chat服务器的消息都发往第一台chat服务器中
};

app.configure('production|development', function(){
	app.route('chat', myRoute);//给chat服务器注册路由
});

channel

用来向用法推送消息的服务

  • 加入channel
remote.add = function(uid, sid, cb){
    var channel = channelService.getChannel('pomelo', true); //得到channel,true在没有的时候创建。
    if(!!channel)
        channel.add(uid, sid);//uid客户id,sid前端服务器id
};
  • 退出channel
handler.kick = function(uid, sid, name){
    var channel = channelService.getChannel(name, false);
    if (!!channel) {
        channel.leave(uid,sid);
    }
};
  • 调用rpc
app.rpc.chat.chatRemote.add(session, uid, app.get('serverId'), function(data){});
  • 推送消息
var channel = channelService.getChannel('pomelo', false);
channel.pushMessage({route:'onChat', msg:'hello'});
  • 客户端监听推送
pomelo.on('onChat', function(data) {});
发布了41 篇原创文章 · 获赞 4 · 访问量 3872

猜你喜欢

转载自blog.csdn.net/weixin_42487874/article/details/105400352