-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
96 lines (64 loc) · 2.4 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
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
from flask import Flask, render_template, request, redirect, session, escape
from flask_socketio import SocketIO, join_room, leave_room, emit
app = Flask(__name__)
app.debug = True
app.secret_key = 'secret'
local = (__name__ == '__main__')
socket = SocketIO(app, manage_session=False)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/temp')
def bar():
lis = open('static/emojis.txt').readlines()
return render_template('setting.html', session=session, emojis_names = lis)
@app.route('/enter/<room>')
def foo(room):
return render_template('enter.html', room=room)
@app.route('/add') # add?user=Rayyan&room=Room
def enter():
room = request.args.get('room','Ming')
user = request.args.get('user','Someone')
return redirect(f'/join/{user}/{room}')
@app.route('/join/<user>/<room>')
def link(user, room):
session['username'] = user or 'Someone'
session['room'] = room or 'Ming'
return redirect('/chat')
@app.route('/chat', methods=['GET', 'POST'])
def chat():
if request.method=='POST':
#Store the data in session
session['username'] = request.form['username']
session['room'] = request.form['room']
elif request.method=='GET':
if 'username' not in session: return redirect('/')
protcl = 'http' if local else 'https'
return render_template('chat.html', session = session,
protcl = protcl, )
@socket.on('join', namespace='/chat')
def join(message):
room = session.get('room', 'Ming')
user = session.get('username', 'Someone')
join_room(room)
emit('status', {'msg': user + ' joined the room.'}, room=room)
@socket.on('text', namespace='/chat')
def text(message):
room = session.get('room')
text = escape(message['msg'])
emit('message', {'msg': text, 'user':session.get('username')}, room=room)
@socket.on('attach', namespace='/chat')
def inform(data):
room = session.get('room')
emit('message', {'msg':str(data),'user':session.get('username') }, room=room)
@socket.on('left', namespace='/chat')
def left(message):
room = session.get('room')
username = session.get('username')
leave_room(room)
session.clear()
emit('status', {'msg': username + ' left.'}, room=room)
if __name__=='__main__':
socket.run(app, debug=True, port=9999)
import webbrowser
webbrowser.open('https://127.0.0.1:9999/')