0

autobahn を使用したパブリッシュ & サブスクライブ モデルでは、特定の のサブスクライバーの数を制限したいと考えてい@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オーバーライドし、内部配列変数を使用して、おそらく独自の を使用してこれを行うことができます...しかし、よりクリーンな方法があるかどうか知りたいです...onClientSubscribedonClientUnsubscribed

  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 '

コードはここにあります。

4

1 に答える 1

1

残念ながら、現在、そのためにサポートされている公開 API はありません。

myWampFactory.getSubscribers(someTopic)のようなものが特定の状況で役立つことに同意します。よろしければ、機能リクエストを追跡できるように、GitHub で問題を報告してください。

あなたが言及した2つの回避策のうち、オーバーライドonClientSubscribedはサブスクリプションの2回目の予約につながるようです。これは、内部にアクセスするよりもさらに満足できないと思います(myFactory.subscriptions)。

于 2013-09-27T13:41:27.330 に答える