单个AppSession无法在同一个topi上订阅和发布

2024-04-20 13:30:44 发布

您现在位置:Python中文网/ 问答频道 /正文

基于简单的helloworld示例,我在发布时将oncounter主题替换为onhello主题。这意味着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主题正在发布,但是没有收到它。在


Tags: fromimportself示例主题defcountertwisted
1条回答
网友
1楼 · 发布于 2024-04-20 13:30:44

原因是,在默认情况下,即使发布者自己订阅了发布到的主题,也不会发布事件。在

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

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

相关问题 更多 >