-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (36 loc) · 1.32 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
from flask import Flask, render_template, request, redirect, url_for
from flask_socketio import SocketIO, join_room, leave_room
app = Flask(__name__)
socketio = SocketIO(app)
room = "main"
@app.route('/')
def home():
return render_template("index.html")
@app.route('/chat')
def chat():
username = request.args.get('username')
# room = request.args.get('room')
if username and room:
return render_template('chat.html', username=username, room=room)
else:
return redirect(url_for('home'))
@socketio.on('send_message')
def handle_send_message_event(data):
app.logger.info(
f"{data['username']} has sent message " +
f"to the room {data['room']}: " +
f"{data['message']}"
)
socketio.emit('receive_message', data, room=data['room'])
@socketio.on('join_room')
def handle_join_room_event(data):
app.logger.info(f"{data['username']} has joined the room {data['room']}")
join_room(data['room'])
socketio.emit('join_room_announcement', data, room=data['room'])
@socketio.on('leave_room')
def handle_leave_room_event(data):
app.logger.info(f"{data['username']} has left the room {data['room']}")
leave_room(data['room'])
socketio.emit('leave_room_announcement', data, room=data['room'])
if __name__ == '__main__':
socketio.run(app, debug=True)