Guido
Guido

Reputation: 319

Autobahn and Twisted, call wamp client method and get result via HTTP

I have done a client like this SomeClient, which talk with a WAMP v1 server.

What I'm unable to do, as you can see on line 25, is to call the SomeClient.i_need_to_call_this method from the HTTPServer class.

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

Upvotes: 1

Views: 441

Answers (1)

oberstet
oberstet

Reputation: 22051

The typical idiom of doing this is to maintain an instance variable factory.proto that will be initialized with None and later filled by the connected protocol to the protocol itself.

By having the factory then available by reference from wherever you want to issue a WAMP call, you can then use factory.proto. You need to guard those uses by if factory.proto .., since you cannot use the client protocol instance when there isn't a connection.

Upvotes: 1

Related Questions