在Publish & Subscribe model using autobahn中,我想限制给定@exportSub(...)
的订阅者数量。你怎么知道订户数量?
(来自examples)
class MyTopicService(object):
def __init__(self, allowedTopicIds):
self.allowedTopicIds = allowedTopicIds
@exportSub("", True)
def subscribe(self, topicUriPrefix, topicUriSuffix):
ret = False
print "client wants to subscribe to %s %s. Allowed topic ids:%s" % (topicUriPrefix, topicUriSuffix, self.allowedTopicIds)
try:
if topicUriSuffix in self.allowedTopicIds:
ret = True
print "Subscribing client to topic %s %s" % (topicUriPrefix, topicUriSuffix)
else:
print "Client not allowed to subscribe to topic %s %s" % (topicUriPrefix, topicUriSuffix)
except:
print "illegal topic - skipped subscription"
finally:
return ret
class MyServerProtocol(WampServerProtocol):
def onSessionOpen(self):
self.registerHandlerForPubSub(MyTopicService(my_keys_1), url_1_foo)
self.registerHandlerForPubSub(MyTopicService(my_keys_2), url_2_bar)
我可以使用我自己的WampServerFactory
执行此操作,覆盖onClientSubscribed
和onClientUnsubscribed
方法并使用内部数组变量...但我想知道是否存在更干净的方式...
class MyFactory(WampServerFactory):
def onClientSubscribed(self, *a, **k):
WampServerFactory.onClientSubscribed(self, a, k)
print '=== client subscribed '
def onClientUnsubscribed(self, *a, **k):
WampServerFactory.onClientUnsubscribed(self, a, k)
print '=== client unsubscribed '
可以找到代码here。
答案 0 :(得分:1)
很遗憾,目前还没有公开支持的API。
我同意像myWampFactory.getSubscribers(someTopic)
这样的东西在某些情况下会有用。如果您在意,请在GitHub上提交问题,以便我们跟踪功能请求。
在你提到的2个工作日中,覆盖onClientSubscribed
似乎导致第二次订阅,我觉得比访问内部(myFactory.subscriptions
)更令人不满意。