0

我刚开始使用 autobahn python,我正在使用辅助ApplicationRunner类来建立基本连接。我的意图是连接一次,发出一条消息,然后退出,因为这就是我正在编写的所有脚本需要做的。我遇到的问题是,一旦启动应用程序,如何关闭应用程序似乎并不明显,我使用这里的建议来访问全局反应器并调用reactor.stop(),但我想知道是否有更好的方法。在下面查看我的脚本,欢迎所有评论。

from autobahn.twisted.wamp import ApplicationRunner, ApplicationSession

class Example(ApplicationSession):
  def __init__(self, config=None):
    ApplicationSession.__init__(self, config)

  def onJoin(self, details):
    self.register(self)
    # publish something here
    from twisted.internet import reactor
    reactor.stop()

runner = ApplicationRunner(u"ws://example_address", u"example_realm")

try:
  runner.run(Example)
except Exception as e:
  print('Couldn\'t connect to WAMP router because: "{}"'.format(e))
4

0 回答 0