如何从协议外发送高速公路/扭曲的WAMP消息?

2024-10-06 12:27:31 发布

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

我遵循the github code中的基本wamp pubsub示例:

此示例从类内部发布消息:

class Component(ApplicationSession):
   """
An application component that publishes an event every second.
"""

   def __init__(self, realm = "realm1"):
      ApplicationSession.__init__(self)
      self._realm = realm


   def onConnect(self):
      self.join(self._realm)


   @inlineCallbacks
   def onJoin(self, details):
      counter = 0
      while True:
         self.publish('com.myapp.topic1', counter)
         counter += 1
         yield sleep(1)

我想创建一个引用,这样我就可以通过这个连接从代码中的其他地方发布消息,例如myobject.myconnection.publish('com.myapp.topic1', 'My message')

从这个类似的question得到的答案似乎是,在连接时,我需要设置类似self.factory.myconnection = self的值。我试过多次这样的排列,但没有成功。在

出厂设置部分如下:

^{pr2}$

我在课堂上设置的参考资料会附在哪里?到client?去transport_factory?到session_factory?在


Tags: theselfcom消息示例initfactorydef
1条回答
网友
1楼 · 发布于 2024-10-06 12:27:31

当你的应用程序会话加入WAMP领域时,它会在应用程序会话工厂中设置对自身的引用:

class MyAppComponent(ApplicationSession):

   ... snip

   def onJoin(self, details):
      if not self.factory._myAppSession:
         self.factory._myAppSession = self

然后可以从代码中的其他地方访问这个会话,例如

^{pr2}$

相关问题 更多 >