WebSocket Server with Autobahn for Remote Procedure Calls

  • Share this:

Code introduction


This function creates a WebSocket server based on the Autobahn library, which accepts two arguments and calls the remote Procedure 'com.myapp.add' with these arguments when a connection is established.


Technology Stack : Autobahn, Twisted

Code Type : Websocket Server Application

Code Difficulty : Intermediate


                
                    
from autobahn.wamp import Application, ApplicationSession, Component
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor

def xxx(arg1, arg2):
    class MyComponent(Component):
        def on_join(self, details):
            print("Connected to router")
            self.session.call("com.myapp.add", arg1, arg2)

    app = Application(MyComponent)
    endpoint = TCP4ServerEndpoint(reactor, 8123)
    reactor.listenTCP(8123, endpoint, factory=app)
    reactor.run()