File size: 1,344 Bytes
947c08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdc95a6
947c08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from datetime import datetime
from channels.generic.websocket import WebsocketConsumer
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
from backend.models.model_cache import SocketRequestChapterQueueCache
from django.dispatch import receiver



class RequestChapter(WebsocketConsumer):
    channel_session_user = True
    
    def __init__(self):
        super().__init__()
        self.room_id = self.__class__.__name__
        
    def connect(self):
        user_socket_id = self.scope['url_route']['kwargs']['socket_id']
        
        async_to_sync(self.channel_layer.group_add)(
            self.room_id,
            self.channel_name,
            
        )
        
        SocketRequestChapterQueueCache.objects.filter(socket_id=user_socket_id).update(channel_name = self.channel_name)
        

        self.accept()
        
        self.send(text_data=json.dumps({
            'type': "socket_info",
            'channel_name': self.channel_name,
        }))

    def event_send(self, event):
        message = event['data']

        self.send(text_data=json.dumps({
            'type': 'event_send',
            'event': message
        }))
        

    def disconnect(self, close_code):
        user_socket_id = self.scope['url_route']['kwargs']['socket_id']