From 7e1176220c76f31f4fa37c38cf9a657f9078db02 Mon Sep 17 00:00:00 2001 From: "Alexie (Boyong) Madolid" Date: Mon, 14 Aug 2023 13:53:42 +0800 Subject: [PATCH] temporary --- jaseci_serv/jaseci_serv/settings.py | 9 ++--- jaseci_serv/jaseci_serv/socket/consumer.py | 35 +++++++++++++------ jaseci_serv/jaseci_serv/socket/routing.py | 2 +- .../templates/examples/social_auth.html | 4 +-- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/jaseci_serv/jaseci_serv/settings.py b/jaseci_serv/jaseci_serv/settings.py index 1c1eda4e6d..a10da3b834 100644 --- a/jaseci_serv/jaseci_serv/settings.py +++ b/jaseci_serv/jaseci_serv/settings.py @@ -101,10 +101,11 @@ CHANNEL_LAYERS = { "default": { - "BACKEND": "channels.layers.InMemoryChannelLayer", - # "CONFIG": { - # "hosts": [("localhost", 6379)], - # }, + # "BACKEND": "channels.layers.InMemoryChannelLayer", + "BACKEND": "channels_redis.core.RedisChannelLayer", + "CONFIG": { + "hosts": [("localhost", 6379)], + }, }, } diff --git a/jaseci_serv/jaseci_serv/socket/consumer.py b/jaseci_serv/jaseci_serv/socket/consumer.py index 4fb18c4fb8..dcef2e155a 100644 --- a/jaseci_serv/jaseci_serv/socket/consumer.py +++ b/jaseci_serv/jaseci_serv/socket/consumer.py @@ -2,29 +2,42 @@ from .event_action import authenticated_user from channels.generic.websocket import WebsocketConsumer from asgiref.sync import async_to_sync +from uuid import uuid4 class SocketConsumer(WebsocketConsumer): def connect(self): self.accept() - - self.token = self.scope["url_route"]["kwargs"]["token"] - - user = authenticated_user(self.token) - if user: - self.token = user.master.urn[9:] - - async_to_sync(self.channel_layer.group_add)(self.token, self.channel_name) - + 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": "connection_established", "message": "Connected!"}) + 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.token, {"type": "notify", "data": data} + self.target, {"type": "notify", "data": data} ) def notify(self, data): diff --git a/jaseci_serv/jaseci_serv/socket/routing.py b/jaseci_serv/jaseci_serv/socket/routing.py index df3e979be9..f65238fdb8 100644 --- a/jaseci_serv/jaseci_serv/socket/routing.py +++ b/jaseci_serv/jaseci_serv/socket/routing.py @@ -2,5 +2,5 @@ from . import consumer websocket_urlpatterns = [ - path(r"ws/socket-server/", consumer.SocketConsumer.as_asgi()) + path(r"ws/socket-server/", consumer.SocketConsumer.as_asgi()) ] diff --git a/jaseci_serv/templates/examples/social_auth.html b/jaseci_serv/templates/examples/social_auth.html index d0eaf155e3..1ebdd49466 100644 --- a/jaseci_serv/templates/examples/social_auth.html +++ b/jaseci_serv/templates/examples/social_auth.html @@ -147,9 +147,9 @@

Google Identity Services Authorization Token model

{% endif %} {% if provider == "google" %} -socket = new WebSocket(`ws://${window.location.host}/ws/socket-server/4554c0c3f228c483481035fbb35995ff4c35dcd5c4b742a03be3a69b65bf6aaf`) +socket = new WebSocket(`ws://${window.location.host}/ws/socket-server/276a40aec1dffc48a25463c3e2545473b45a663364adf3a2f523b903aa254c9f`) {% else %} -socket = new WebSocket(`ws://${window.location.host}/ws/socket-server/123456`) +socket = new WebSocket(`ws://${window.location.host}/ws/socket-server/anonymous`) {% endif %} socket.onmessage = (event) => { console.log(event)