您的位置:首页 > 其它

Pomelo游戏服务器端开发系列(2)-pushMessage

2017-05-08 00:00 211 查看
摘要: 消息通知方法介绍

推送消息方法

pushMessage

以channel为单位推送消息, 每个channel可比作一个房间。

代码示例

/**
* Push message to all the members in the channel
*
* @param {String} route message route
* @param {Object} msg message that would be sent to client
* @param {Object} opts user-defined push options, optional
* @param {Function} cb callback function
*/

var channelService = pomelo.app.get('channelService');
var channel = channelService.getChannel(channelName, false);
var params = {
route:  'onChat',
msg:    {},
};

channel.pushMessage(params);

pushMessageByUids

该方法只在channelService中定义, channel不可使用该方法。

推送消息给指定的用户集合uids:[{uid:1001,sid:'connector-server-1'},{uid:1002,sid:'connector-server-2'}].

代码示例

/**
* Push message by uids.
* Group the uids by group. ignore any uid if sid not specified.
*
* @param {String} route message route
* @param {Object} msg message that would be sent to client
* @param {Array} uids the receiver info list, [{uid: userId, sid: frontendServerId}]
* @param {Object} opts user-defined push options, optional
* @param {Function} cb cb(err)
* @memberOf ChannelService
*/

var channelService = pomelo.app.get('channelService');

channelService.pushMessageByUids(route, msg, uids, opt, callback);

broadcast

广播消息。 推送给所有用户

代码示例

/**
* Broadcast message to all the connected clients.
*
* @param  {String}   stype      frontend server type string
* @param  {String}   route      route string
* @param  {Object}   msg        message
* @param  {Object}   opts       user-defined broadcast options, optional
*                               opts.binded: push to binded sessions or all the sessions
*                               opts.filterParam: parameters for broadcast filter.
* @param  {Function} cb         callback
* @memberOf ChannelService
*/

var channelService = pomelo.app.get('channelService');
//ex: stype -> connector
channelService.broadcast(stype, route, msg, opts, callback)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Pomelo
相关文章推荐