3

基于简单的 Hello World 示例,我在发布时将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主题正在发布,但没有收到。

4

1 回答 1

3

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

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

yield self.publish('com.example.onhello', counter,
   options = autobahn.wamp.types.PublishOptions(excludeMe = False))
于 2014-11-17T11:03:31.093 回答