2014-11-16 18 views
3

基於簡單的Hello World示例,我在發佈時將oncounter主題替換爲onhello。這意味着AppSession正在訂閱它自己發佈的主題。我猜它應該能夠接收它自己的消息,但它看起來沒有。有沒有辦法做到這一點?單個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話題被髮表,但沒有收到。

回答

2

原因是,默認情況下,即使發佈者自己訂閱發佈到的主題,發佈者也不會獲得發佈的事件。

您可以通過改變這種行爲提供了一個options參數publish()

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

好,我看,這不是很明顯......那現在是有道理的,謝謝! –