-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (46 loc) · 1.65 KB
/
app.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
from flask import Flask, render_template, url_for, request, redirect, session
from flask_socketio import SocketIO, join_room, leave_room, emit
from flask_session import Session
# FLASK_APP = app.py
# FLASK_ENV = development
# FLASK_DEBUG = 0
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'secret_key'
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)
socketio = SocketIO(app, manage_session=False)
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
@app.route('/chat', methods=['GET', 'POST'])
def chat():
if request.method == 'POST':
username = request.form['username']
room = request.form['room']
session['username'] = username
session['room'] = room
return render_template('chat.html', session=session)
else:
if session.get('username') is not None:
return render_template('chat.html', session=session)
else:
return redirect(url_for('index'))
@socketio.on('join', namespace='/chat')
def join():
room = session.get('room')
join_room(room)
emit('status', {'msg': session.get('username') + ' has entered the room.'}, room=room)
@socketio.on('text', namespace='/chat')
def text(message):
room = session.get('room')
emit('message', {'msg': session.get('username') + ' : ' + message['msg']}, room=room)
@socketio.on('left', namespace='/chat')
def left(message):
room = session.get('room')
username = session.get('username')
leave_room(room)
session.clear()
emit('status', {'msg': username + ' has left the room.'}, room=room)
if __name__ == '__main__':
socketio.run(app)