-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
129 lines (96 loc) · 3.3 KB
/
main.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
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
122
123
124
125
126
127
128
129
from flask import Flask, request, session, redirect, render_template, url_for
from flask_socketio import SocketIO, send, join_room, leave_room
import random
from string import ascii_uppercase
from dotenv import load_dotenv
import os
load_dotenv()
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)
rooms = {}
def generate_unique_code(length=4):
code = ''.join(random.choices(ascii_uppercase, k=length))
if code in rooms:
return generate_unique_code(length)
return code
@app.route('/', methods=['GET', 'POST'])
def home():
session.clear()
if request.method == 'POST':
name = request.form.get('name')
code = request.form.get('code')
join = request.form.get('join', False)
create = request.form.get('create', False)
if not name:
return render_template('home.html',
error="Pls enter a name.",
name=name,
code=code,)
if join != False and not code:
return render_template('home.html',
error="Pls enter a room code.",
name=name,
code=code,)
room = code
if create != False:
room = generate_unique_code()
rooms[room] = {'members': 0, 'messages': []}
elif code not in rooms:
return render_template('home.html',
error="Room not exists.",
name=name,
code=code,)
session['name'] = name
session['room'] = room
return redirect(url_for('room'))
return render_template('home.html')
@app.route('/room/')
def room():
room = session.get('room')
name = session.get('name')
if room is None or name is None or room not in rooms:
return redirect(url_for('home'))
return render_template('room.html', room=room, messages=rooms[room]['messages'])
@socketio.on('connect')
def connect(auth):
room = session.get('room')
name = session.get('name')
if not room or not name:
return
if room not in rooms:
leave_room(room)
return
join_room(room)
send({'name': name, 'message': 'has entered the room.'}, to=room)
rooms[room]['members'] += 1
print(f'{name} has entered the room {room}.')
@socketio.on('disconnect')
def disconnect():
room = session.get('room')
name = session.get('name')
leave_room(room)
if room in rooms:
rooms[room]['members'] -= 1
if rooms[room]['members'] <= 0:
del rooms[room]
send({'name': name, 'message': 'has left the room.'}, to=room)
print(f'{name} has left the room {room}.')
@socketio.on('message')
def message(data):
room = session.get('room')
if room not in rooms:
return
name = session.get('name')
message = data['message'].strip()
if not message:
return
content = {
'name': name,
'message': message
}
send(content, to=room)
rooms[room]['messages'].append(content)
print(f'{name} said: {message}')
if __name__ == "__main__":
socketio.run(app, debug=True)