1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
from autobahn.wamp import Api
# create an API object to use the decorator style
# register/subscribe WAMP actions
api = Api()
@api.register(u'com.example.add2')
def add2(a, b):
return a + b
@api.subscribe(u'com.example.on-hello', details=True)
def on_hello(msg, details=None):
print(u'Someone said: {}'.format(msg))
details.session.leave()
@coroutine
def component1(reactor, session):
"""
A first component, which gets called "setup-like". When
it returns, this signals that the component is ready for work.
"""
# expose the API on the session
yield session.expose(api)
@coroutine
def component2(reactor, session):
"""
A second component, which gets called "main-like".
When it returns, this will automatically close the session.
"""
result = yield session.call(u'com.example.add2', 2, 3)
session.publish(u'com.example.on-hello', u'result={}'.format(result))
if __name__ == '__main__':
from autobahn.twisted.component import Component, run
# Components wrap either a setup or main function and
# can be configured with transports, authentication and so on.
components = [
Component(setup=component1),
Component(main=component2)
]
# a convenience runner is provided which takes a list of
# components and runs all of them
run(components)
|