Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
guo xiaoyong
Jumpserver
Commits
9b99af13
Commit
9b99af13
authored
3 years ago
by
ibuler
Browse files
Options
Download
Email Patches
Plain Diff
perf: 重构通知的ws架构
parent
07898004
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
apps/notifications/const.py
+0
-0
apps/notifications/const.py
apps/notifications/events.py
+27
-0
apps/notifications/events.py
apps/notifications/ws.py
+35
-1
apps/notifications/ws.py
with
62 additions
and
1 deletion
+62
-1
apps/notifications/const.py
0 → 100644
+
0
-
0
View file @
9b99af13
This diff is collapsed.
Click to expand it.
apps/notifications/events.py
0 → 100644
+
27
-
0
View file @
9b99af13
from
functools
import
wraps
from
common.utils.connection
import
RedisPubSub
from
django.utils.functional
import
LazyObject
class
UserEventSubPub
(
LazyObject
):
def
_setup
(
self
):
self
.
_wrapped
=
lambda
:
RedisPubSub
(
'notifications.SiteMessageCome'
)
class
Event
:
def
__init__
(
self
,
user
,
tp
,
msg
):
self
.
user
=
user
self
.
name
=
''
self
.
type
=
tp
self
.
msg
=
msg
def
on_user_event_ws_connect
(
func
):
@
wraps
(
func
)
def
wrapper
(
*
args
,
**
kwargs
):
event
=
func
(
*
args
,
**
kwargs
)
return
event
return
wrapper
user_event_chan
=
UserEventSubPub
()
This diff is collapsed.
Click to expand it.
apps/notifications/ws.py
+
35
-
1
View file @
9b99af13
...
...
@@ -4,13 +4,47 @@ import json
from
channels.generic.websocket
import
JsonWebsocketConsumer
from
common.utils
import
get_logger
from
.models
import
SiteMessage
from
.site_msg
import
SiteMessageUtil
from
.signals_handler
import
new_site_msg_chan
logger
=
get_logger
(
__name__
)
class
UserEventWebsocket
(
JsonWebsocketConsumer
):
disconnected
=
False
refresh_every_seconds
=
10
def
connect
(
self
):
user
=
self
.
scope
[
"user"
]
if
user
.
is_authenticated
:
self
.
accept
()
thread
=
threading
.
Thread
(
target
=
self
.
send_event
)
thread
.
start
()
else
:
self
.
close
()
def
receive
(
self
,
text_data
=
None
,
bytes_data
=
None
,
**
kwargs
):
data
=
json
.
loads
(
text_data
)
refresh_every_seconds
=
data
.
get
(
'refresh_every_seconds'
)
try
:
refresh_every_seconds
=
int
(
refresh_every_seconds
)
except
Exception
as
e
:
logger
.
error
(
e
)
return
if
refresh_every_seconds
>
0
:
self
.
refresh_every_seconds
=
refresh_every_seconds
def
send_event
(
self
):
user_id
=
self
.
scope
[
"user"
].
id
def
disconnect
(
self
,
close_code
):
self
.
disconnected
=
True
self
.
close
()
class
SiteMsgWebsocket
(
JsonWebsocketConsumer
):
disconnected
=
False
refresh_every_seconds
=
10
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment