How to send Autobahn/Twisted WAMP message from outside of protocol?

后端 未结 1 1169
慢半拍i
慢半拍i 2021-01-12 17:13

I am following the basic wamp pubsub examples in the github code:

This example publishes messages from within the class:

class Component(ApplicationS         


        
相关标签:
1条回答
  • 2021-01-12 17:48

    Upon your app session joining the WAMP realm, it sets a reference to itself on the app session factory:

    class MyAppComponent(ApplicationSession):
    
       ... snip
    
       def onJoin(self, details):
          if not self.factory._myAppSession:
             self.factory._myAppSession = self
    

    You then can access this session from elsewhere in your code, e.g.

       @inlineCallbacks
       def pub():
          counter = 0  
          while True:
             ## here we can access the app session that was created ..
             ##
             if session_factory._myAppSession:
                session_factory._myAppSession.publish('com.myapp.topic123', counter)
                print("published event", counter)
             else:
                print("no session")
             counter += 1
             yield sleep(1)
    
       pub()
    
    0 讨论(0)
提交回复
热议问题