This repository was archived by the owner on Feb 23, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathapplication.py
More file actions
121 lines (86 loc) · 3.2 KB
/
Copy pathapplication.py
File metadata and controls
121 lines (86 loc) · 3.2 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os, redis, time, json
from flask import Flask, render_template, request, session, make_response, jsonify
from flask_session import Session
from flask_socketio import SocketIO, emit
if os.getenv("FLASK_ENV") == "development":
db = redis.Redis('localhost', decode_responses=True)
else:
redis_url = os.getenv('REDIS_URL')
db = redis.Redis('redis://h:pb3023e0d946b086371cdea7b53538ad69d0d83b3f9d4618df6ea83c2f60ab8bb@ec2-99-81-103-115.eu-west-1.compute.amazonaws.com:9589', decode_responses=True)
app = Flask(__name__)
app.config["SECRET_KEY"] = "secret"
socketio = SocketIO(app)
# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = True
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Define message class
class Message:
def __init__(self, sender, time, text):
self.sender = sender
self.time = time
self.text = text
def json(self):
j = {"sender": self.sender,
"time": self.time,
"text": self.text}
return json.dumps(j)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/channels", methods=["POST"])
def channels():
username = request.form.get("username")
if not db.exists('channels'):
# Create default channel list
db.rpush('channels', "General")
channels = db.lrange('channels', 0, -1)
return render_template("channels.html", title="Channels", channels=channels, username=username, activeChannel="General")
# Create a new channel and push to websocket
@socketio.on("create channel")
def create_channel(channel_name):
channels = db.lrange('channels', 0, -1)
if (channel_name in channels):
emit("channel already exists", channel_name, broadcast=True)
else:
# Update channels on Redis
db.rpush('channels', channel_name)
channels = db.lrange('channels', 0, -1)
# Push message
emit("channel added", channel_name, broadcast=True)
# Delete channel
@socketio.on("delete channel")
def delete_channel(channel_name):
# Delete item from Redis
db.lrem('channels', 1, channel_name)
# Delete Redis list for this channel (i.e. clear all messages)
db.delete(channel_name)
# Push message
emit("channel deleted", channel_name, broadcast=True)
# Open chat
@socketio.on("open chat")
def open_chat(channel_name):
print("Opening chat"+channel_name)
# Load chat history from Redis
if db.exists(channel_name):
chat = db.lrange(channel_name, 0, -1)
else:
chat = ""
# Push message
emit("chat opened", chat, broadcast=False)
# Send message
@socketio.on("send message")
def send_message(channel_name, message, username):
m = Message(sender=username, time=time.time(), text=message)
# Push message to Redis and trim store to 100 messages
db.rpush(channel_name, m.json())
db.ltrim(channel_name, -99, -1)
# Push message
emit("message received", m.json(), broadcast=True)
# Typing
@socketio.on("typing")
def send_message(channel_name, username, contents):
d = {"username":username, "channel_name": channel_name, "contents": contents}
data = json.dumps(d)
# Push message
emit("user is typing", data, broadcast=True)