-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhandlers.py
51 lines (34 loc) · 1.08 KB
/
handlers.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
# Contains event handlers for flask socket io events
from flask_socketio import join_room, leave_room, send
from flask import session
try:
from __main__ import socketio
except ImportError:
from server import socketio
@socketio.on('join')
def on_join_room(data):
'''
on call user will be added to the room sent by the user
'''
username = data['username']
room = data['room']
join_room(room)
send({"user":session["user"],"message":"has enterd the chat"}, to=room,broadcast=True)
@socketio.on('leave')
def on_leave(data):
'''
on call user will be removed from the room
'''
username = data['username']
room = data['room']
leave_room(room)
send({"user":session["user"],"message":"has left the chat"}, to=room,broadcast=True)
@socketio.on('message')
def handel_message(json):
'''
messages from users will be broadcasted
'''
room = json["room"]
message = json["message"]
if message != "User Connected":
send({"user":session["user"],"message":message},room=room,broadcast=True)