-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE-REQUEST]: Websocket via channels[daphne]
- Loading branch information
Showing
9 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from json import loads, dumps | ||
from .event_action import authenticated_user | ||
from channels.generic.websocket import WebsocketConsumer | ||
from asgiref.sync import async_to_sync | ||
from uuid import uuid4 | ||
from jaseci_serv.base.models import lookup_global_config | ||
from channels.layers import settings | ||
|
||
|
||
class SocketConsumer(WebsocketConsumer): | ||
def connect(self): | ||
self.accept() | ||
session_id = None | ||
authenticated = False | ||
target = self.scope["url_route"]["kwargs"]["target"] | ||
|
||
if target == "anonymous": | ||
self.target = session_id = str(uuid4()) | ||
else: | ||
user = authenticated_user(target) | ||
if user: | ||
self.target = user.master.urn[9:] | ||
authenticated = True | ||
else: | ||
self.target = session_id = str(uuid4()) | ||
|
||
async_to_sync(self.channel_layer.group_add)(self.target, self.channel_name) | ||
self.send( | ||
text_data=dumps( | ||
{ | ||
"type": "connect", | ||
"authenticated": authenticated, | ||
"session_id": session_id, | ||
} | ||
) | ||
) | ||
|
||
def receive(self, text_data=None, bytes_data=None): | ||
data = loads(text_data) | ||
|
||
async_to_sync(self.channel_layer.group_send)( | ||
self.target, {"type": "notify", "data": data} | ||
) | ||
|
||
def notify(self, data): | ||
self.send(text_data=dumps(data)) | ||
|
||
|
||
setattr( | ||
settings, | ||
"CHANNEL_LAYERS", | ||
{ | ||
"default": loads( | ||
lookup_global_config( | ||
"CHANNEL_LAYER", | ||
{ | ||
"BACKEND": "channels_redis.core.RedisChannelLayer", | ||
"CONFIG": { | ||
"hosts": [("localhost", 6379)], | ||
}, | ||
}, | ||
) | ||
) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
try: | ||
from hmac import compare_digest | ||
except ImportError: | ||
|
||
def compare_digest(a, b): | ||
return a == b | ||
|
||
|
||
import binascii | ||
from knox.crypto import hash_token | ||
from knox.models import AuthToken | ||
from knox.settings import CONSTANTS | ||
from jaseci.jsorc.live_actions import jaseci_action | ||
from channels.layers import get_channel_layer | ||
from asgiref.sync import async_to_sync | ||
from django.utils import timezone | ||
|
||
|
||
def authenticated_user(token: str): | ||
for auth_token in AuthToken.objects.filter( | ||
token_key=token[: CONSTANTS.TOKEN_KEY_LENGTH] | ||
): | ||
try: | ||
digest = hash_token(token) | ||
if ( | ||
compare_digest(digest, auth_token.digest) | ||
and auth_token.expiry > timezone.now() | ||
): | ||
return auth_token.user | ||
except (TypeError, binascii.Error): | ||
pass | ||
return None | ||
|
||
|
||
@jaseci_action(act_group=["wb"]) | ||
def notify(target: str, data: dict): | ||
async_to_sync(get_channel_layer().group_send)( | ||
target, {"type": "notify", "data": data} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.urls import path | ||
from . import consumer | ||
|
||
websocket_urlpatterns = [ | ||
path(r"ws/socket-server/<str:target>", consumer.SocketConsumer.as_asgi()) | ||
] |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters