-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (39 loc) · 1.43 KB
/
app.py
File metadata and controls
59 lines (39 loc) · 1.43 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
import os
from datetime import datetime
from flask import Flask, render_template, jsonify, request
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config["SECRET_KEY"] = 'secret!'
socketio = SocketIO(app)
channels = []
@app.route('/')
def index():
return render_template('index.html', channels=channels)
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/channel/<string:name>')
def channel(name):
for channel in channels:
if channel['name'] == name:
return render_template('channel.html', channel=channel, counter=len(channel['messages']))
return render_template('index.html')
@socketio.on('create channel')
def create(data):
channels.append({'name': data['name'], 'author': data['author'], 'messages': []})
emit('new channel', {'name': data['name']}, broadcast=True)
@socketio.on('send message')
def message(data):
new_message = {'author': data['author'],
'timestamp': datetime.now().strftime('%H:%M'),
'message': data['message']}
for channel in channels:
if channel['name'] == data['channel']:
messages = channel['messages']
message_number = len(messages)
messages.append(new_message)
if message_number > 100:
messages.pop(0)
emit('new message', new_message, broadcast=True)
if __name__ == '__main__':
socketio.run(app)