| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 'use strict';
- var quick = require('quick-pomelo');
- var P = quick.Promise;
- var push = quick.controllers.push;
- var _joinAsync = P.coroutine(function* (channelId, playerId, connectorId) {
- this.channels[channelId] = this.channels[channelId] || {};
- var channel = this.channels[channelId];
- channel[playerId] = connectorId;
- });
- var _quitAsync = P.coroutine(function* (channelId, playerId) {
- var channel = this.channels[channelId] || {};
- if (channel[playerId]) {
- delete channel[playerId];
- }
- });
- var _pushAsync = P.coroutine(function* (channelId, playerIds, route, msg) {
- var channel = this.channels[channelId] || {};
- var connectorUids = {};
- if (playerIds) {
- playerIds.forEach((playerId) => {
- var connectorId = channel[playerId];
- if (connectorId) {
- if (!connectorUids[connectorId]) {
- connectorUids[connectorId] = [];
- }
- connectorUids[connectorId].push(playerId);
- }
- });
- }
- else {
- Object.keys(channel).forEach((playerId) => {
- var connectorId = channel[playerId];
- if (connectorId) {
- if (!connectorUids[connectorId]) {
- connectorUids[connectorId] = [];
- }
- connectorUids[connectorId].push(playerId);
- }
- });
- }
- var pushMsg = { msg: msg, route: route };
- Object.keys(connectorUids).forEach((connectorId) => {
- if (!this.msgBuff.hasOwnProperty(connectorId)) {
- this.msgBuff[connectorId] = [];
- }
- this.msgBuff[connectorId].push({
- uids: connectorUids[connectorId],
- route: route,
- msg: pushMsg
- });
- });
- });
- module.exports = function (app) {
- var _push = push(app);
- var serverType = app.getServerType();
- var gameServers = ['game'];
- if (-1 != gameServers.indexOf(serverType)) {
- _push.channels = {};
- _push._joinAsync = _joinAsync;
- _push._quitAsync = _quitAsync;
- _push._pushAsync = _pushAsync;
- }
- _push.broadcastAsync = broadcastAsync;
- return _push;
- };
- function broadcastAsync(route, msg) {
- var app = this.app;
- var namespace = 'sys';
- var service = 'channelRemote';
- var method = 'broadcast';
- var servers = this.app.getServersByType('connector');
- var opts = { type: 'broadcast', userOptions: {} };
- opts.isBroadcast = true;
- if (!!opts.userOptions) {
- opts.binded = opts.userOptions.binded;
- opts.filterParam = opts.userOptions.filterParam;
- }
- var broadMsg = { msg: msg, route: route };
- var stats = [];
- for (let i = 0; i < servers.length; ++i) {
- stats.push(P.promisify(app.rpcInvoke, app)(servers[i].id, {
- namespace: namespace,
- service: service,
- method: method,
- args: [route, broadMsg, opts]
- }));
- }
- return P.all(stats);
- };
|