如何从协议之外发送高速公路/扭曲 WAMP 消息? [英] How to send Autobahn/Twisted WAMP message from outside of protocol?

查看:34
本文介绍了如何从协议之外发送高速公路/扭曲 WAMP 消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 github 代码:

这个例子从类中发布消息:

This example publishes messages from within the class:

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')

I want to create a reference so that I can publish messages over this connection from elsewhere in the code, i.e. myobject.myconnection.publish('com.myapp.topic1', 'My message')

来自这个类似的问题答案似乎是在连接时,我需要设置类似 self.factory.myconnection = self 的内容.我已经尝试了多次排列但没有成功.

From this similar question the answer seems to be that upon connection, I need to set something like self.factory.myconnection = self. I have tried multiple permutations of this without success.

出厂设置部分如下:

   ## create a WAMP application session factory
   ##
   from autobahn.twisted.wamp import ApplicationSessionFactory
   session_factory = ApplicationSessionFactory()


   ## .. and set the session class on the factory
   ##
   session_factory.session = Component


   ## create a WAMP-over-WebSocket transport client factory
   ##
   from autobahn.twisted.websocket import WampWebSocketClientFactory
   transport_factory = WampWebSocketClientFactory(session_factory, args.wsurl, debug = args.debug)
   transport_factory.setProtocolOptions(failByDrop = False)


   ## start a WebSocket client from an endpoint
   ##
   client = clientFromString(reactor, args.websocket)
   client.connect(transport_factory)

我在班级中设置的任何参考资料会附加到哪里?客户端?transport_factory?session_factory?

Where would any reference that I set from within the class be attached? to client? to transport_factory? to session_factory?

推荐答案

在您的应用会话加入 WAMP 领域后,它会在应用会话工厂中设置对自身的引用:

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()

这篇关于如何从协议之外发送高速公路/扭曲 WAMP 消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆