microservice3.py
1 |
###############################################################################
|
---|---|
2 |
#
|
3 |
# The MIT License (MIT)
|
4 |
#
|
5 |
# Copyright (c) Crossbar.io Technologies GmbH
|
6 |
#
|
7 |
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8 |
# of this software and associated documentation files (the "Software"), to deal
|
9 |
# in the Software without restriction, including without limitation the rights
|
10 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
# copies of the Software, and to permit persons to whom the Software is
|
12 |
# furnished to do so, subject to the following conditions:
|
13 |
#
|
14 |
# The above copyright notice and this permission notice shall be included in
|
15 |
# all copies or substantial portions of the Software.
|
16 |
#
|
17 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
23 |
# THE SOFTWARE.
|
24 |
#
|
25 |
###############################################################################
|
26 |
|
27 |
import asyncio |
28 |
from os import environ |
29 |
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner |
30 |
|
31 |
|
32 |
class Component(ApplicationSession): |
33 |
"""
|
34 |
An application component that subscribes and receives events, and
|
35 |
stop after having received 5 events.
|
36 |
"""
|
37 |
|
38 |
async def onJoin(self, details): |
39 |
|
40 |
self.received = 0 |
41 |
|
42 |
def on_event(i): |
43 |
print("Got event: {}".format(i))
|
44 |
self.received += 1 |
45 |
if self.received > 5: |
46 |
self.leave()
|
47 |
|
48 |
await self.subscribe(on_event, u'com.myapp.cipot1') |
49 |
|
50 |
def onDisconnect(self): |
51 |
asyncio.get_event_loop().stop() |
52 |
|
53 |
|
54 |
if __name__ == '__main__': |
55 |
runner = ApplicationRunner( |
56 |
environ.get("AUTOBAHN_DEMO_ROUTER", u"ws://127.0.0.1:8080/ws"), |
57 |
u"realm1",
|
58 |
) |
59 |
runner.run(Component) |