-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.py
37 lines (25 loc) · 900 Bytes
/
chat.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
"""
A chat server using WebSockets.
gunicorn chat:websockets --config=gunicorn_settings.py
"""
import json
import flask
import websocket_channels
REDIS_URL = 'redis://127.0.0.1:6379/0'
app = flask.Flask(__name__)
websockets = websocket_channels.WebSocketChannelMiddleware(app, REDIS_URL)
@app.route('/')
def home_view():
return flask.render_template('home.html')
@app.route('/channel/<path:channel>')
def channel_view(channel):
return flask.render_template('channel.html', channel=channel.rstrip('/'))
@app.route('/publish', methods=('POST',))
def publish_messages_view():
"""Publish given messages on the given channels. The POST body should be in form of
{channel: message, ...}
"""
data = json.loads(flask.request.get_data())
for channel, message in data.iteritems():
websockets.publish_message(message, channel)
return flask.Response('OK')