Skip to content

Commit

Permalink
temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
amadolid committed Aug 14, 2023
1 parent d9ec40b commit 7e11762
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
9 changes: 5 additions & 4 deletions jaseci_serv/jaseci_serv/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
},
},
}

Expand Down
35 changes: 24 additions & 11 deletions jaseci_serv/jaseci_serv/socket/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion jaseci_serv/jaseci_serv/socket/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from . import consumer

websocket_urlpatterns = [
path(r"ws/socket-server/<str:token>", consumer.SocketConsumer.as_asgi())
path(r"ws/socket-server/<str:target>", consumer.SocketConsumer.as_asgi())
]
4 changes: 2 additions & 2 deletions jaseci_serv/templates/examples/social_auth.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ <h1>Google Identity Services Authorization Token model</h1>
{% 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)
Expand Down

0 comments on commit 7e11762

Please sign in to comment.