-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
raven_server.py
52 lines (45 loc) · 1.55 KB
/
raven_server.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
#!/usr/bin/env python
"""
// -------------------------------------------------------------
// author Giga
// project qeeqbox/raven
// email [email protected]
// description tornado, websockets and redis (minimal)
// licensee AGPL-3.0
// -------------------------------------------------------------
// contributors list qeeqbox/raven/graphs/contributors
// -------------------------------------------------------------
"""
from os import path
from random import randint
from websockets import client, serve
from aioredis import from_url
from contextlib import suppress
from tornado import websocket, web, ioloop, gen
from json import dumps
class WebSocketHandler(websocket.WebSocketHandler):
def check_origin(self, origin):
return True
def on_close(self):
self.running = False
async def open(self):
print(str(self.request))
self.running = True
pub_redis = await from_url('redis://localhost:6379')
pubsub = pub_redis.pubsub()
await pubsub.subscribe("alerts")
with suppress(Exception):
while True and self.running:
message = await pubsub.get_message(ignore_subscribe_messages=True)
if message is not None:
if message["data"] == b"STOP":
break
await self.write_message(message['data'])
await gen.sleep(1)
await pubsub.unsubscribe("alerts")
await pub_redis.close()
def on_message(self,message):
pass
application = web.Application([(r'/websocket', WebSocketHandler),(r'/(.*)', web.StaticFileHandler, {'path': path.dirname(__file__)})])
application.listen(4751)
ioloop.IOLoop.instance().start()