编程语言
首页 > 编程语言> > python-单个AppSession无法订阅和发布同一主题

python-单个AppSession无法订阅和发布同一主题

作者:互联网

基于简单的Hello World示例,我在发布时用onhello替换了oncounter主题.这意味着AppSession订阅了它本身正在发布的主题.我猜它应该能够接收自己的消息,但看起来好像没有.有没有办法做到这一点?

对于可重现的示例:


from twisted.internet.defer import inlineCallbacks

from autobahn.twisted.util import sleep from autobahn.twisted.wamp import ApplicationSession

class AppSession(ApplicationSession):

@inlineCallbacks
def onJoin(self, details):

    def onhello(msg):
        print("event for 'onhello' received: {}".format(msg))
    sub = yield self.subscribe(onhello, 'com.example.onhello')

    counter = 0
    while True:

        yield self.publish('com.example.onhello', counter)
        print("published to 'onhello' with counter {}".format(counter))
        counter += 1

        yield sleep(1)

运行crossbar start之后,我看到正在发布onhello主题,但是没有收到它.

解决方法:

原因是,默认情况下,即使发布者本身订阅了发布的主题,发布者也不会发布事件.

您可以通过为publish()提供一个options参数来更改该行为:

yield self.publish('com.example.onhello', counter,
   options = autobahn.wamp.types.PublishOptions(excludeMe = False))

标签:autobahn,python,crossbar
来源: https://codeday.me/bug/20191121/2048737.html