每个channel与server一一对应,如果要跨server,则需要引入pomelo-globalchannel-plugin和pomelo-status-plugin两个插件,以下remote代码是原生态channel,rpc调用时只能在指定的一个server上操作,否则会出现取不到用户的情况
/** * 消息推送 * Created by lmiky on 14-4-16. */ var RemoteMsgPush = require('../../../domain/remoteMsgPush'); var RemoteMsgPushUser = require('../../../domain/remoteMsgPushUser'); var remoteMsgPushService = require('../../../services/remoteMsgPushService'); var remoteMsgPushUserService = require('../../../services/remoteMsgPushUserService'); var dateUtils = require('../../../util/dateUtils'); var logUtils = require('../../../util/logUtils'); var utils = require('../../../util/utils'); var SystemConfig = require('../../../../config/systemConfig'); var Code = require('../../../../config/code'); var log = logUtils.getLogger(__filename); var self = null; module.exports = function(app) { return new Handler(app); }; var Handler = function(app) { this.app = app; this.channelService = this.app.get('channelService'); this.globalChannelService = this.app.get('globalChannelService'); self = this; }; /** * 添加用户 * @param uid * @param cb */ Handler.prototype.add = function(connectorServiceId, uid, cb) { try { //加入到globalchannel /*this.globalChannelService.add(SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, uid, connectorServiceId, function(err) { if(err) { utils.invokeCallback(cb, err); } else { //因为globalchannel无法对单个推送信息,只能向全部用户推送,所以必须再用到原生的channel var channel = self.channelService.getChannel(SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, true); channel.add(uid, connectorServiceId); utils.invokeCallback(cb, null); } });*/ var channel = self.channelService.getChannel(SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, true); channel.add(uid, connectorServiceId); utils.invokeCallback(cb, null); }catch(e) { log.error('scheduler.messageRemote.add exception ' + e.stack); utils.invokeCallback(cb, e); } } /** * 添加推送消息 * @param opts 参数 * @param cb */ Handler.prototype.addMessage = function(opts, cb) { try { //检查参数 if(!opts || !opts.msg || !opts.msg.content) { utils.invokeCallback(cb, new Error('参数错误'), 0); return; } //消息内容 var content = opts.msg.content; //内容 var title = opts.msg.title; //标题 if(!title) { title = content; opts.msg.title = title; } var pushTime = new Date(); opts.msg.pushTime = pushTime;//推送时间 var expiredTime = opts.msg.expiredTime; //过期时间,为空表示永不过期 if(!!expiredTime) { //重新转换,防止出现不同系统时间时区不一致,比如web-api系统的时区跟game-server的时区就差了8个小时 expiredTime = dateUtils.parseTimeByDefault(dateUtils.formatTimeByDefault(expiredTime)); opts.msg.expiredTime = expiredTime; } var pushType = SystemConfig.MSG_PUSH.PUSH_TYPE_TERM; //推送类别 var offline = opts.msg.offline; //是否离线 if(!offline || (offline != SystemConfig.MSG_PUSH.OFFLINE_YES && offline != SystemConfig.MSG_PUSH.OFFLINE_NO)) { offline = SystemConfig.MSG_PUSH.OFFLINE_YES; //默认离线 } var receiver = opts.receiver; //接受者,如果为空表示向所有人推送 if(!!receiver) { //多个接收者之间以“,”分隔 receiver = receiver.split(','); } var remoteMsgPush = new RemoteMsgPush({title: title, content: content, pushTime: pushTime, expiredTime: expiredTime, pushType: pushType, offline: offline, receiver: opts.receiver}); //添加消息 remoteMsgPushService.add(remoteMsgPush, function(err, remoteMsgPush) { if(err) { utils.invokeCallback(cb, err, 0); return; } //推送消息 self.pushMessage(opts.msg, receiver, remoteMsgPush.id, function(err, pushUserLength){ if(err) { utils.invokeCallback(cb, err, 0); } else { utils.invokeCallback(cb, null, pushUserLength); } }); }); }catch(e) { log.error('connector.connectorRemote.addMessage exception ' + e.stack); utils.invokeCallback(cb, e, 0); } } /** * 向用户推送消息 * @param msg * @param uids 用户id数组,如果为空表示向全部用户推送 * @param cb */ Handler.prototype.pushMessage = function(msg, uids, pushId, cb) { try { //检查参数 if(!msg || !msg.title || !msg.content || !msg.pushTime) { utils.invokeCallback(cb, new Error('参数错误'), 0); return; } //消息内容 var title = msg.title; //标题 var content = msg.content; //内容 var pushTime = dateUtils.formatTimeByDefault(msg.pushTime);//推送时间 var remoteMsgPush = {title: title, content: content, pushTime: pushTime}; var frontendServerType = SystemConfig.MSG_PUSH.FRONTEND_SERVER_TYPE; //向指定用户推送 if(!!uids && uids.length > 0) { var channel = self.channelService.getChannel(SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, false); if(!!channel) { var targetUids = []; var index = 0; for(var i=0; i<uids.length; i++) { //判断推送的用户是否在线 var member = channel.getMember(uids[i]); if(!!member) { targetUids[index] = {uid: uids[i], sid: member['sid']}; index++; } } if(targetUids.length == 0) { //当前无在线用户 utils.invokeCallback(cb, null, 0); return; } //推送 self.channelService.pushMessageByUids('onMsg', remoteMsgPush, targetUids, function(err) { if(err) { log.error('scheduler.messageRemote.pushMessage pushMessageByUids exception ' + err.stack); utils.invokeCallback(cb, err, 0); return; } var now = new Date(); for(var i=0; i<targetUids.length; i++) { try { //保存已发送信息 var pushUser = new RemoteMsgPushUser({uid: targetUids[i].uid, pushId: pushId, receiveTime: now}); remoteMsgPushUserService.add(pushUser, function(err, remoteMsgPushUser) { if(err) { log.error('scheduler.messageRemote.pushMessage add pushUser[' + pushUser.uid + '-' + pushId + '] exception: ' + err.stack); } }); } catch(addPushUserErr) { log.error('scheduler.messageRemote.pushMessage add pushUser[' + targetUids[i].uid + '-' + pushId + '] exception: ' + addPushUserErr.stack); } } utils.invokeCallback(cb, err, targetUids.length); }); } else { //当前无在线用户 utils.invokeCallback(cb, null, 0); return; } } else { //向全部用户推送 //获取用户列表 /*self.globalChannelService.getMembersByChannelName(frontendServerType, SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, function(err, users) { if(err) { log.error('scheduler.messageRemote.pushMessage getMembersByChannelName exception ' + err.stack); utils.invokeCallback(cb, err, 0); return; } if(users.length == 0) { utils.invokeCallback(cb, null, 0); return; } //推送消息 self.globalChannelService.pushMessage(frontendServerType, 'onMsg', remoteMsgPush, SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, null, function(error) { if(error) { log.error('scheduler.messageRemote.pushMessage pushMessage exception ' + error.stack); utils.invokeCallback(cb, error, 0); return; } else { var now = new Date(); for(var i=0; i<users.length; i++) { try { //保存已发送信息 var pushUser = new RemoteMsgPushUser({uid: users[i], pushId: pushId, receiveTime: now}); remoteMsgPushUserService.add(pushUser, function(err, remoteMsgPushUser) { if(err) { log.error('scheduler.messageRemote.pushMessage add pushUser[' + pushUser.uid + '-' + pushId + '] exception: ' + err.stack); } }); } catch(addPushUserErr) { log.error('scheduler.messageRemote.pushMessage add pushUser[' + users[i] + '-' + pushId + '] exception: ' + addPushUserErr.stack); } } utils.invokeCallback(cb, null, users.length); } }); });*/ var channel = self.channelService.getChannel(SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, false); if(!channel) { utils.invokeCallback(cb, null, 0); return; } var users = channel.getMembers(); if(users.length == 0) { utils.invokeCallback(cb, null, 0); return; } channel.pushMessage('onMsg', remoteMsgPush, null, function(err) { if(err) { log.error('scheduler.messageRemote.pushMessage pushMessage exception ' + err.stack); utils.invokeCallback(cb, err, 0); return; } else { var now = new Date(); for(var i=0; i<users.length; i++) { try { //保存已发送信息 var pushUser = new RemoteMsgPushUser({uid: users[i], pushId: pushId, receiveTime: now}); remoteMsgPushUserService.add(pushUser, function(err, remoteMsgPushUser) { if(err) { log.error('scheduler.messageRemote.pushMessage add pushUser[' + pushUser.uid + '-' + pushId + '] exception: ' + err.stack); } }); } catch(addPushUserErr) { log.error('scheduler.messageRemote.pushMessage add pushUser[' + users[i] + '-' + pushId + '] exception: ' + addPushUserErr.stack); } } utils.invokeCallback(cb, null, users.length); } }); } }catch(e) { log.error('connector.connectorRemote.pushMessage exception ' + e.stack); utils.invokeCallback(cb, e, 0); } } /** * 退出渠道 * @param uid * @param sid * @param cb */ Handler.prototype.leave = function(uid, sid, cb) { //globalChannel /*self.globalChannelService.leave(SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, uid, sid, function(err) { if(err) { log.error('删除globalChannel里的用户[%s]失败: %j', uid, err.stack); } else { log.info('删除globalChannel里的用户[%s]成功', uid); } //原生态channel var channel = self.channelService.getChannel(SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, false); if( !! channel) { channel.leave(uid, sid); } utils.invokeCallback(cb, err); });*/ var channel = self.channelService.getChannel(SystemConfig.MSG_PUSH.GLOBAL_CHANNEL_NAME, false); if( !! channel) { channel.leave(uid, sid); } utils.invokeCallback(cb, null); };