This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
2020-11-26 16:08:26 +00:00
|
|
|
"""websocket Message consumer"""
|
|
|
|
from channels.generic.websocket import JsonWebsocketConsumer
|
|
|
|
from django.core.cache import cache
|
|
|
|
|
|
|
|
|
|
|
|
class MessageConsumer(JsonWebsocketConsumer):
|
|
|
|
"""Consumer which sends django.contrib.messages Messages over WS.
|
|
|
|
channel_name is saved into cache with user_id, and when a add_message is called"""
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
self.accept()
|
2020-11-26 16:57:46 +00:00
|
|
|
cache.set(f"user_{self.scope['user'].pk}_messages_{self.channel_name}", True)
|
2020-11-26 16:08:26 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def disconnect(self, close_code):
|
2020-11-26 16:57:46 +00:00
|
|
|
cache.delete(f"user_{self.scope['user'].pk}_messages_{self.channel_name}")
|
2020-11-26 16:08:26 +00:00
|
|
|
|
|
|
|
def event_update(self, event: dict):
|
|
|
|
"""Event handler which is called by Messages Storage backend"""
|
|
|
|
self.send_json(event)
|