-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_websocket.py
70 lines (55 loc) · 1.81 KB
/
flask_websocket.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
# -*- coding: utf-8 -*-
"""
flask_websocket.py
~~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2017 by Damon Chen.
:license: BSD, see LICENSE for more details.
"""
import json
from collections import defaultdict
from flask import request, current_app as app
class WebSocket(object):
def __init__(self, app=None):
self.app = app
self.event_handlers = defaultdict(list)
self.raw_message_handler = None
if app is not None:
self.init_app(app)
def init_app(self, app):
url = app.config.get('WEBSOCKET_URL', '/ws')
@app.route(url)
def ws():
socket = request.environ.get('wsgi.websocket')
if socket is not None:
while True:
message = socket.receive()
if not message:
break
self.dispatch_message(message)
def on(self, event):
def decorator(handler):
# add to current event handler
self.event_handlers[event].append(handler)
return handler
return decorator
def on_raw_message(self):
def decorator(handler):
self.raw_message_handler = handler
return handler
return decorator
def dispatch(self, event, *args, **kwargs):
for handler in self.event_handlers[event]:
handler(*args, **kwargs)
def dispatch_message(self, message):
if self.raw_message_handler is not None:
self.raw_message_handler(message)
else:
try:
msg = json.loads(message)
except Exception as e:
app.logger.error(e)
else:
event = msg.get('event')
self.dispatch(event, msg.get('data'))
finally:
pass