-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevents.py
59 lines (49 loc) · 1.56 KB
/
events.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
from datetime import datetime
from channels.message import Message
from ..models import Room
def user_join(message, **kwargs): # type: (Message, dict)
"""
Handles a user joining a room
:param message: The websocket message
:param kwargs: Route kwargs
:return:
"""
room = Room.objects.get(slug=message.content.pop('slug'))
username = message.content.pop('username')
room.add_member(
username=username,
reply_channel_name=message.content.pop('reply_channel_name')
)
room.emit(
event='user-join',
data={
'members': room.members(),
'username': username,
}
)
def user_leave(message, **kwargs): # type: (Message, dict)
"""
Handles when a user leaves the room
"""
room = Room.objects.get(slug=message.content.pop('slug'))
left_member = room.remove_member(reply_channel_name=message.reply_channel.name)
# Send the user_leave message to the members in the room
room.emit(
event='user-leave',
data={
'members': room.members(),
'username': left_member.username
})
def client_send(message, **kwargs): # type: (Message, dict)
"""
Handles when the client sends a message
"""
room = Room.objects.get(slug=message.content.pop('slug'))
# Send the new message to the room
room.emit(
event='message-new',
data={
'msg': message.content['msg'],
'username': message.content['username'],
'time': datetime.now().strftime('%I:%M:%S %p')
})