1

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()
Emile Cormier
  • 28,391
  • 15
  • 94
  • 122
Guido
  • 319
  • 2
  • 9
  • create an object of `SomeClient`in `render_GET` method and call the function. – salmanwahed Jul 29 '14 at 18:24
  • SomeClient runs independently, and also starting it inside HTTPServer _initi_'s, factory doesn't bind the SomeClient class methods. – Guido Jul 29 '14 at 18:58

1 Answers1

1

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.

oberstet
  • 21,353
  • 10
  • 64
  • 97