perf: 在线用户根据websocket添加用户是否活跃状态

This commit is contained in:
feng
2023-11-08 16:49:38 +08:00
committed by Bryan
parent 7746491e19
commit 5ab8ff4fde
3 changed files with 9 additions and 6 deletions

View File

@@ -1,7 +1,7 @@
import json
from channels.generic.websocket import JsonWebsocketConsumer
from django.core.cache import caches
from django.core.cache import cache
from common.db.utils import safe_db_connection
from common.utils import get_logger
@@ -21,7 +21,8 @@ class SiteMsgWebsocket(JsonWebsocketConsumer):
if user.is_authenticated:
self.accept()
session = self.scope['session']
caches.sadd(WS_SESSION_KEY, session.session_key)
redis_client = cache.client.get_client()
redis_client.sadd(WS_SESSION_KEY, session.session_key)
self.sub = self.watch_recv_new_site_msg()
else:
self.close()
@@ -66,4 +67,5 @@ class SiteMsgWebsocket(JsonWebsocketConsumer):
return
self.sub.unsubscribe()
session = self.scope['session']
caches.srem(WS_SESSION_KEY, session.session_key)
redis_client = cache.client.get_client()
redis_client.srem(WS_SESSION_KEY, session.session_key)