This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublisher.py
98 lines (81 loc) · 4.05 KB
/
publisher.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.util import sleep
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner
from autobahn.wamp.exception import ApplicationError
class AppSession(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
## SUBSCRIBE to a topic and receive events
##
# def onhello(msg):
# print("event for 'onhello' received: {}".format(msg))
# sub = yield self.subscribe(onhello, 'com.example.onhello')
# print("subscribed to topic 'onhello'")
## REGISTER a procedure for remote calling
##
# def add2(x, y):
# print("add2() called with {} and {}".format(x, y))
# return x + y
# reg = yield self.register(add2, 'com.example.add2')
# print("procedure add2() registered")
## PUBLISH and CALL every second .. forever
##
counter = 0
questions = [
{'id': '4kjhs34j43', 'question': 'Which package manager is used to install Polymer elements?'},
{'id': '9qx3n85mpl', 'question': '1 + 1 = ?'},
{'id': 'j2h4jhn2b4', 'question': 'Which day of week is today?'},
{'id': '0v3afl12o5', 'question': 'When was Record Evolution founded?'},
{'id': '1p43n242jx', 'question': 'Which Polymer Element can be used to collect user input?'},
{'id': '10eumc4m4m', 'question': 'Which polymer template can be used to repeat html content?'},
{'id': '498hff3h3k', 'question': 'Is this the last question?'}
]
while True:
## PUBLISH an event
##
# yield self.publish('re.meetup.question', counter)
print("published to 'oncounter' with counter {}".format(counter))
counter += 1
# if counter < 16:
# yield self.publish('re.meetup.question', questions[0])
# elif counter < 32:
# yield self.publish('re.meetup.question', questions[1])
# elif counter < 64:
# yield self.publish('re.meetup.question', questions[2])
# elif counter < 96:
# yield self.publish('re.meetup.question', questions[3])
# elif counter < 128:
# yield self.publish('re.meetup.question', questions[4])
# elif counter < 160:
# yield self.publish('re.meetup.question', questions[5])
# else:
# yield self.publish('re.meetup.question', questions[6])
if counter < 600:
yield self.publish('re.meetup.question', questions[0])
elif counter < 1200:
yield self.publish('re.meetup.question', questions[1])
elif counter < 1800:
yield self.publish('re.meetup.question', questions[2])
elif counter < 2400:
yield self.publish('re.meetup.question', questions[3])
elif counter < 3000:
yield self.publish('re.meetup.question', questions[4])
elif counter < 3600:
yield self.publish('re.meetup.question', questions[5])
else:
yield self.publish('re.meetup.question', questions[6])
## CALL a remote procedure
##
# try:
# res = yield self.call('com.example.mul2', counter, 3)
# print("mul2() called with result: {}".format(res))
# except ApplicationError as e:
## ignore errors due to the frontend not yet having
## registered the procedure we would like to call
# if e.error != 'wamp.error.no_such_procedure':
# raise e
yield sleep(1)
if __name__ == '__main__':
from autobahn.twisted.wamp import ApplicationRunner
runner = ApplicationRunner(url =u"ws://127.0.0.1:8099/ws", realm=u"polymer_meetup")
runner.run(AppSession)