我有一個消費者class BingoConsumer(WebsocketConsumer): logged_in = 0 def connect(self): async_to_sync(self.channel_layer.group_add)( "login", self.channel_name ) self.accept() def disconnect(self, close_code): async_to_sync(self.channel_layer.group_discard)( "login", self.channel_name ) self.logged_in -= 1 def receive(self, text_data): text_data = json.loads(text_data) if text_data['type'] == 'login': self.logged_in += 1 async_to_sync(self.channel_layer.group_send)( "login", { 'type': 'login', 'count': self.logged_in, } ) def login(self, event): self.send(text_data=json.dumps({ 'type': 'login', 'total': event['count'], }))每次用戶登錄我的網站時都會調用它,它會自動調用類型為“登錄”的 websocket。我想跟蹤當前登錄的用戶數量,但目前無論有多少人登錄,它仍然是 1。這讓我想知道,Django 是否將消費者視為單一使用類?它會根據需要創建它們并銷毀它們嗎?
添加回答
舉報
0/150
提交
取消
