mirror of https://github.com/jumpserver/jumpserver
perf: 在线用户根据websocket添加用户是否活跃状态
parent
7746491e19
commit
5ab8ff4fde
|
@ -4,7 +4,7 @@ from datetime import timedelta
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.cache import caches
|
from django.core.cache import caches, cache
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
@ -278,7 +278,8 @@ class UserSession(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_active(self):
|
def is_active(self):
|
||||||
return caches.sismember(WS_SESSION_KEY, self.key)
|
redis_client = cache.client.get_client()
|
||||||
|
return redis_client.sismember(WS_SESSION_KEY, self.key)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def date_expired(self):
|
def date_expired(self):
|
||||||
|
|
|
@ -177,7 +177,7 @@ class UserSessionSerializer(serializers.ModelSerializer):
|
||||||
fields_mini = ['id']
|
fields_mini = ['id']
|
||||||
fields_small = fields_mini + [
|
fields_small = fields_mini + [
|
||||||
'type', 'ip', 'city', 'user_agent', 'user', 'is_current_user_session',
|
'type', 'ip', 'city', 'user_agent', 'user', 'is_current_user_session',
|
||||||
'backend', 'backend_display', 'date_created', 'date_expired'
|
'backend', 'backend_display', 'is_active', 'date_created', 'date_expired'
|
||||||
]
|
]
|
||||||
fields = fields_small
|
fields = fields_small
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from channels.generic.websocket import JsonWebsocketConsumer
|
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.db.utils import safe_db_connection
|
||||||
from common.utils import get_logger
|
from common.utils import get_logger
|
||||||
|
@ -21,7 +21,8 @@ class SiteMsgWebsocket(JsonWebsocketConsumer):
|
||||||
if user.is_authenticated:
|
if user.is_authenticated:
|
||||||
self.accept()
|
self.accept()
|
||||||
session = self.scope['session']
|
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()
|
self.sub = self.watch_recv_new_site_msg()
|
||||||
else:
|
else:
|
||||||
self.close()
|
self.close()
|
||||||
|
@ -66,4 +67,5 @@ class SiteMsgWebsocket(JsonWebsocketConsumer):
|
||||||
return
|
return
|
||||||
self.sub.unsubscribe()
|
self.sub.unsubscribe()
|
||||||
session = self.scope['session']
|
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)
|
||||||
|
|
Loading…
Reference in New Issue