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

推送消息方法

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)

转载于:https://my.oschina.net/nextzeus/blog/894962

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值