-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.py
72 lines (55 loc) · 1.83 KB
/
ws.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
import tornado
from tornado.websocket import WebSocketHandler
from tornado.web import RequestHandler, Application, url
import tornadis
clients = []
class GetHandler(RequestHandler):
@tornado.gen.coroutine
def get(self):
self.render("websocket.html")
class BiddingHandler(RequestHandler):
@tornado.gen.coroutine
def put(self, id):
print "id: %s" % id
price = self.get_argument("price", None)
result = yield client.call("PUBLISH","item:%s"%id,price)
print "item:%s was bid %s" % (id,price)
if not isinstance(result,tornadis.TornadisException):
self.write("Bid :%s" % price)
self.finish()
class WSHandler(WebSocketHandler):
@tornado.gen.coroutine
def initialize(self):
self.redis = tornadis.PubSubClient(autoconnect=False)
yield self.redis.connect()
result = yield self.redis.pubsub_psubscribe("item:*")
print result
loop = tornado.ioloop.IOLoop.current()
loop.add_callback(self.watch_redis)
@tornado.gen.coroutine
def watch_redis(self):
while True:
print "..."
msg = yield self.redis.pubsub_pop_message()
if isinstance(msg, tornadis.TornadisException):
break
else:
self.write_message(msg[-1])
self.redis.disconnect()
def open(self, *args):
pass
@tornado.gen.coroutine
def on_message(self, message):
pass
def on_close(self):
print "close"
self.redis.disconnect()
app = Application([
url(r"/", GetHandler),
url(r"/bid/([0-9]+)", BiddingHandler),
url(r"/ws", WSHandler)
])
client = tornadis.Client(host="localhost",port=6379, autoconnect=True)
if __name__ == '__main__':
app.listen(8888)
tornado.ioloop.IOLoop.current().start()