问题描述
我想将非 MONGO-DB 数据从服务器发布者广播到客户端集合.目前我保存了所有注册的订阅者句柄以用于发布数据
I want to broadcast NON-MONGO-DB data from a server publisher to client collections. Currently I save all registered subscriber handles to use those for posting the data
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 向这些客户端发送数据,例如:
Then I can use the all_handles somewhere in my app to send data to those clients, like:
function broadcast(msg) {
all_handles.forEach(function(handle) {
handle.added("data", Random.id(), msg);
}
}
这已经在使用中并且正在运行.
This is already in use and running.
问:我要寻找的是:我可以从当前已经存在的流星(内部)对象(如 _sessions 或其他东西)获取所有句柄吗?
Q: What I am looking for is: Can I get all handles from currently already existing meteor (internal) objects like _sessions or something else?
如果不用我一个人去组织所有时间的订阅者就好了.
It would be great if I had not to organize the subscribers handle all the time by myself.
请不要回答其他广播包的链接,如流媒体或其他.我想继续使用标准集合,但代码尽可能少.
Please do not answer with links to other broadcast packages like streamy or else. I want to continue with standard collections but with as less code as possible.
感谢您的帮助和反馈汤姆
Thanks for help and feedbackTom
推荐答案
根据@laberning 的通知,我现在使用了未记录的"流星连接.
As informed by @laberning I used for now the "undocumented" meteor connections.
您可以发布给所有订阅者的发布方法,例如:
You can post to all subscribers of a publishing method like:
// 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 用于发布、订阅和广播消息
这篇关于流星 - 获取发布者方法的所有订阅者会话句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!