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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
from twisted.internet.task import react
from twisted.internet.defer import inlineCallbacks as coroutine
from autobahn.twisted.wamp import Connection
# A single session can freeze and resume over different transports
# sessions have a lifecycle independent of Connection/Transport
session = ApplicationSession()
def add2(a, b):
return a + b
@coroutine
def main(transport):
# join a realm and try to resume the session
details = yield session.join(transport, u'myrealm1', resume=True)
if not details.is_resumed:
# if the session is fresh, register a procedure ..
yield session.register(u'com.myapp.add2', add2)
# and leave the realm, freezing the session
yield session.leave(freeze=True)
else:
# if the session is resumed, our registration will have been
# reestablished automatically
result = yield session.call(u'com.myapp.add2', 2, 3)
print("Result: {}", result)
# leave the realm finally
yield session.leave()
yield transport.close()
@coroutine
def test():
transports = [
{
'type': 'rawsocket',
'serializer': 'msgpack',
'endpoint': {
'type': 'unix',
'path': '/tmp/cb1.sock'
}
},
{
'type': 'websocket',
'url': 'ws://127.0.0.1:8080/ws',
'endpoint': {
'type': 'tcp',
'host': '127.0.0.1',
'port': 8080
}
}
]
connection1 = Connection(main1, transports=transports[0])
yield react(connection1.start)
connection2 = Connection(main2, transports=transports[1])
yield react(connection2.start)
if __name__ == '__main__':
test()
|