【问题标题】:meteor - get all subscriber session handles for a publisher methodmeteor - 获取发布者方法的所有订阅者会话句柄
【发布时间】:2015-10-24 01:52:18
【问题描述】:

我想将 NON-MONGO-DB 数据从服务器发布者广播到客户端集合。目前我保存所有已注册的订阅者句柄以用​​于发布数据

client.js:

col = new Meteor.Collection("data")

Meteor.subscribe("stream")

在服务器端看起来像

server.js

all_handles = [];

Meteor.publish("stream", function() {
  // safe reference to this sessions
  var self = this;
  // save reference to this subscriber
  all_handles.push(self);
  // signal ready
  self.ready();
  // on stop subscription remove this handle from list
  self.onStop(function() {
    all_handles = _.without(all_handles, self); 
  }
}

然后我可以在我的应用程序中的某处使用 all_handles 向这些客户端发送数据,例如:

function broadcast(msg) {
  all_handles.forEach(function(handle) {
    handle.added("data", Random.id(), msg);
  }
}

这已经在使用并正在运行。

问:我正在寻找的是:我能否从当前已经存在的流星(内部)对象(如 _sessions 或其他对象)中获取所有句柄?

如果不是我一直自己组织订阅者的处理,那就太好了。

请不要回答其他广播包的链接,如streamy或其他。我想继续使用标准集合,但代码尽可能少。

感谢您的帮助和反馈 汤姆

【问题讨论】:

    标签: meteor


    【解决方案1】:

    也许这对你有用:https://stackoverflow.com/a/30814101/2005564

    您可以通过var connections = Meteor.server.stream_server.open_sockets; 获得连接,但正如 looshi 所说,这可能会因未来的流星更新而中断,因为它不是公共 API 的一部分...

    【讨论】:

    • 感谢您的帖子。我想知道对于这个简单的要求是否没有可靠的千年发展目标解决方案。
    【解决方案2】:

    根据@laberning 的通知,我现在使用“未记录的”流星连接。

    您可以发布给所有订阅者的发布方法,例如:

    // publish updated values to all subscribers
    function publish_to_all_subscribers(subscription_name, id, data) {
      _.each(Meteor.server.stream_server.open_sockets, function(connection) {
        _.each(connection._meteorSession._namedSubs, function(sub) {
          if (sub._name == subscription_name) {
            sub.insert(subscription_name, id, data);
          }
        })
      });
    }
    
    // create stream publisher
    Meteor.publish('stream', function(){
      // set ready
      this.ready();
    });
    
    ...
    // use publishing somewhere in your app
    publish_to_all_subscribers('stream', Random.id(), {msg: "Hello to all"});
    ...
    

    更新:查看示例MeteorPad for Publish and Subscribe and Broadcast messages

    【讨论】:

    • 如果有人知道 Meteor 的官方属性,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    • 2016-12-14
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    相关资源
    最近更新 更多