class ChatConsumer(AsyncWebsocketConsumer): async def connect(self): query_string_bytes = self.scope.get('query_string', b'') # 1. Get the raw query string from the scope query_string_str = query_string_bytes.decode('utf-8') # 2. Decode it from bytes to string (UTF-8 is a common encoding) query_params = parse_qs(query_string_str) # 3. Parse the query string into a dictionary token_list = query_params.get('token', []) # 4. Extract the token value token = token_list[0] if token_list else None user = await get_user_from_token(token) self.room_name = self.scope[ 'url_route' ][ 'kwargs' ][ 'room_name' ] self.room_group_name = f'chat_ {self.room_name} ' # Authenticate user if isinstance (user, AnonymousUser): await self.close() else : self.user = user # Join room group await self.channel_layer.group_add( self.room_group_name, self.channel_name ...
Comments
Post a Comment