【问题标题】:Can autobahn.twisted.wamp.Application do pub/sub?autobahn.twisted.wamp.Application 可以发布/订阅吗?
【发布时间】:2014-08-15 16:29:16
【问题描述】:

我想使用一些 pub/sub 功能以及来自 autobahn.twisted.wamp.Application 的 rpc

如果我可以不这样做的话,我宁愿不创建 ApplicationSession 类。

注册的 rpc 方法会导致客户端订阅和发布吗? 如果可以,请告诉我怎么做。

【问题讨论】:

    标签: python twisted autobahn wamp-protocol


    【解决方案1】:

    是的,当然:

    def onEvent(msg):
       print("got event: {}".format(msg))
    
    @app.register('com.example.triggersubscribe')
    def triggerSubscribe():
       yield app.session.subscribe(onEvent, 'com.example.topic1')
    

    triggerSubscribe 被调用时(例如从另一个WAMP 组件远程调用),被调用者(暴露com.example.triggersubscribe 的WAMP 组件)将动态订阅com.example.topic1

    当然,您也可以在注册过程中发布:app.session.publish()

    我已经添加了完整的例子(包括JS客户端)here

    【讨论】:

    • 您如何做到这一点并让triggerSubscribe 将值返回给调用者? Python 禁止在生成器中使用return 语句。
    • 在 Twisted/Python2 中,使用 returnValue (twistedmatrix.com/documents/current/api/…)。 Trollius/Python2 和 asyncio/Python3 有模拟机制。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2017-05-01
    • 2017-03-11
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多