refactored consumers.py

This commit is contained in:
rizwanisready
2024-06-21 17:49:18 +05:30
parent 1ed0d78fa6
commit a459742801

View File

@@ -1,18 +1,10 @@
from channels.generic.websocket import AsyncWebsocketConsumer, WebsocketConsumer from channels.generic.websocket import AsyncWebsocketConsumer
import json import json
import django
django.setup()
from accounts.models import IAmPrincipal
from channels.exceptions import StopConsumer from channels.exceptions import StopConsumer
from asgiref.sync import async_to_sync, sync_to_async
from django.utils import timezone from django.utils import timezone
from chat.models import ChatGroup, ChatMessage from chat.models import ChatGroup, ChatMessage
from channels.db import database_sync_to_async from channels.db import database_sync_to_async
from django.db import close_old_connections
from rest_framework_simplejwt.tokens import AccessToken
from rest_framework_simplejwt.authentication import JWTAuthentication from rest_framework_simplejwt.authentication import JWTAuthentication
import threading
class ChatConsumer(AsyncWebsocketConsumer): class ChatConsumer(AsyncWebsocketConsumer):
@@ -26,15 +18,6 @@ class ChatConsumer(AsyncWebsocketConsumer):
print("token_key: ", token_key) print("token_key: ", token_key)
self.user = await self.get_user_async(token_key) self.user = await self.get_user_async(token_key)
print("self.user: ", self.user) print("self.user: ", self.user)
# Start the thread to get the user object
# user_thread = threading.Thread(target=self.get_user_async, args=(token_key,))
# user_thread.start()
# # Wait for the thread to finish and assign the user object to the scope
# user_thread.join()
# self.scope["user"] = self.user
# print("User: ", self.scope["user"])
# print("self.user: ", self.user)
# Join room group # Join room group
await self.channel_layer.group_add(self.room_name, self.channel_name) await self.channel_layer.group_add(self.room_name, self.channel_name)