-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_catcher.py
69 lines (53 loc) · 2 KB
/
event_catcher.py
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
__author__ = 'romick'
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from twisted.python.failure import Failure
from autobahn.wamp import subscribe
from autobahn.twisted.wamp import ApplicationSession
DEBUG = True
class EventCatcher(ApplicationSession):
"""
An application component that subscribes and receives events
"""
@inlineCallbacks
def onJoin(self, details):
print("event catcher session attached")
# subscribe all methods on this object decorated with "@wamp.subscribe"
# as PubSub event handlers
#
results = yield self.subscribe(self)
# check we didn't have any errors
for sub in results:
if isinstance(sub, Failure):
print("subscribe failed:", sub.getErrorMessage())
@subscribe(u'wamp.metaevent.session.on_join')
def on_command(self, msg):
self.printer(u'wamp.metaevent.session.on_join', msg)
@subscribe(u'com.tetry.run_command')
def on_command(self, msg):
self.printer(u'com.tetry.run_command', msg)
@subscribe(u'com.tetry.log')
def on_log(self, msg):
self.printer(u'com.tetry.log', msg)
@subscribe(u'com.tetry.got_from_com')
def on_com_line(self, msg):
self.printer(u'com.tetry.got_from_com', msg)
@subscribe(u'com.tetry.servo_targets')
def on_com_line(self, msg):
self.printer(u'com.tetry.servo_targets', msg)
@subscribe(u'com.tetry.sent2com')
def on_com_line(self, msg):
self.printer(u'com.tetry.sent2com', msg)
# @wamp.subscribe()
# def on_com_line(self, msg):
# self.printer(, msg)
def printer(self, event, msg):
print("Event catcher: {}: {}".format(event, msg))
def onDisconnect(self):
print("disconnected")
reactor.stop()
if __name__ == '__main__':
from autobahn.twisted.wamp import ApplicationRunner
runner = ApplicationRunner("ws://127.0.0.1:8080/ws", "realm1")
print "started"
runner.run(EventCatcher)