pomelo消息推送

每个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


你可能感兴趣的:(nodejs,pomelo)