【问题标题】:Autobahn and Twisted, call wamp client method and get result via HTTPAutobahn 和 Twisted,调用 wamp 客户端方法并通过 HTTP 获取结果
【发布时间】:2014-07-29 17:59:36
【问题描述】:

我做过一个像 SomeClient 这样的客户端,它与 WAMP v1 服务器通信。

正如您在第 25 行看到的那样,我无法做的是从 HTTPServer 类调用 SomeClient.i_need_to_call_this 方法。

from twisted.internet import reactor
from twisted.web import server, resource
from autobahn.wamp1.protocol import WampClientFactory, WampCraClientProtocol
from autobahn.twisted.websocket import connectWS

class SomeClient(WampCraClientProtocol):
    def __init__(self):
        pass

    def doSomething(self, something):
        return something

    def i_need_to_call_this(self):
        d = self.call("http://somewhere")
        d.addCallback(self.doSomething)



class HTTPServer(resource.Resource):
    isLeaf = True

    def render_GET(self, request):
        request.setHeader("content-type", "application/json")
        result = "Here i need to call SomeClient.i_need_to_call_this and render the result"
        return result


if __name__ == '__main__':
    factory = WampClientFactory("wss://someurl")
    factory.protocol = SomeClient
    connectWS(factory)
    reactor.listenTCP(8080, server.Site(HTTPServer()))
    reactor.run()

【问题讨论】:

  • render_GET方法中创建SomeClient的对象并调用函数。
  • SomeClient 独立运行,也在 HTTPServer init 内部启动,工厂不绑定 SomeClient 类方法。

标签: python twisted autobahn wamp-protocol


【解决方案1】:

这样做的典型习惯用法是维护一个实例变量factory.proto,该变量将使用None 进行初始化,然后由连接的协议填充到协议本身。

通过让 factory 在您想要发出 WAMP 呼叫的任何位置作为参考可用,您就可以使用 factory.proto。你需要保护if factory.proto .. 的这些使用,因为当没有连接时你不能使用客户端协议实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多