pomelo訊息推送

來源:互聯網
上載者:User

每個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無法對單個推送資訊,只能向全部使用者推送,所以必須再用到原生的channelvar 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);}//原生態channelvar 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);};


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.