在使用高速公路的发布和订阅模型中,我想限制给定订阅者的数量@exportSub(...)
。你怎么知道订阅者的数量?
(来自示例)
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
andonClientUnsubscribed
方法并使用内部数组变量来做到这一点......但我想知道是否有更清洁的方法......
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 '
代码可以在这里找到。