push.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. var quick = require('quick-pomelo');
  3. var P = quick.Promise;
  4. var push = quick.controllers.push;
  5. var _joinAsync = P.coroutine(function* (channelId, playerId, connectorId) {
  6. this.channels[channelId] = this.channels[channelId] || {};
  7. var channel = this.channels[channelId];
  8. channel[playerId] = connectorId;
  9. });
  10. var _quitAsync = P.coroutine(function* (channelId, playerId) {
  11. var channel = this.channels[channelId] || {};
  12. if (channel[playerId]) {
  13. delete channel[playerId];
  14. }
  15. });
  16. var _pushAsync = P.coroutine(function* (channelId, playerIds, route, msg) {
  17. var channel = this.channels[channelId] || {};
  18. var connectorUids = {};
  19. if (playerIds) {
  20. playerIds.forEach((playerId) => {
  21. var connectorId = channel[playerId];
  22. if (connectorId) {
  23. if (!connectorUids[connectorId]) {
  24. connectorUids[connectorId] = [];
  25. }
  26. connectorUids[connectorId].push(playerId);
  27. }
  28. });
  29. }
  30. else {
  31. Object.keys(channel).forEach((playerId) => {
  32. var connectorId = channel[playerId];
  33. if (connectorId) {
  34. if (!connectorUids[connectorId]) {
  35. connectorUids[connectorId] = [];
  36. }
  37. connectorUids[connectorId].push(playerId);
  38. }
  39. });
  40. }
  41. var pushMsg = { msg: msg, route: route };
  42. Object.keys(connectorUids).forEach((connectorId) => {
  43. if (!this.msgBuff.hasOwnProperty(connectorId)) {
  44. this.msgBuff[connectorId] = [];
  45. }
  46. this.msgBuff[connectorId].push({
  47. uids: connectorUids[connectorId],
  48. route: route,
  49. msg: pushMsg
  50. });
  51. });
  52. });
  53. module.exports = function (app) {
  54. var _push = push(app);
  55. var serverType = app.getServerType();
  56. var gameServers = ['game'];
  57. if (-1 != gameServers.indexOf(serverType)) {
  58. _push.channels = {};
  59. _push._joinAsync = _joinAsync;
  60. _push._quitAsync = _quitAsync;
  61. _push._pushAsync = _pushAsync;
  62. }
  63. _push.broadcastAsync = broadcastAsync;
  64. return _push;
  65. };
  66. function broadcastAsync(route, msg) {
  67. var app = this.app;
  68. var namespace = 'sys';
  69. var service = 'channelRemote';
  70. var method = 'broadcast';
  71. var servers = this.app.getServersByType('connector');
  72. var opts = { type: 'broadcast', userOptions: {} };
  73. opts.isBroadcast = true;
  74. if (!!opts.userOptions) {
  75. opts.binded = opts.userOptions.binded;
  76. opts.filterParam = opts.userOptions.filterParam;
  77. }
  78. var broadMsg = { msg: msg, route: route };
  79. var stats = [];
  80. for (let i = 0; i < servers.length; ++i) {
  81. stats.push(P.promisify(app.rpcInvoke, app)(servers[i].id, {
  82. namespace: namespace,
  83. service: service,
  84. method: method,
  85. args: [route, broadMsg, opts]
  86. }));
  87. }
  88. return P.all(stats);
  89. };