daphne
daphne copied to clipboard
Exception inside application: 'WebSocketProtocol' object has no attribute 'handshake_deferred'
Got this during or just after the failure state I've been encountering in https://github.com/django/channels_redis/issues/81:
(sorry, there are two exceptions smashed together there)
It appears this would happen if you somehow tried to accept or reject before you got a websocket.connect
message (not sure how that's possible). I'll get a look at it eventually, but I can't imagine it happens a lot?
It seems like some exceptions can get scilenced in async workers. Instead of seeing the actual exception you see this.
Little late now, but I received this error because I made a typo in my websocket_receive() function. I just changed:
async def websocket_receive(self, event): await self.send({ "type": "websocket.accept", "text": event["text"] })
to:
async def websocket_receive(self, event): await self.send({ "type": "websocket.send", "text": event["text"] })
Hi, I get the same error but I can't figure out where it comes from. @agronick would you simply recommend adding try/except then ?
I did exactly like @garyjh126 did. It solved my problem.
In my case, it was because I called await self.accept()
twice in the connect
method of my channels AsyncJsonWebsocketConsumer. Removing the first call solved the problem. I wish this specific situation could display a more helpful error message.
My case was the same as @incidentist, thanks for pointing that out!
And i agree, the error message should be more clear.
@incidentist This was exactly what happened to me
import json import logging from channels.generic.websocket import AsyncWebsocketConsumer from django.contrib.auth import get_user_model from .services import ChatService
User = get_user_model() logger = logging.getLogger('django')
class ChatConsumer(AsyncWebsocketConsumer): def init(self, *args, **kwargs): super().init(*args, **kwargs) self.room_group_name = None
async def connect(self):
try:
print("Connect method called")
await self.accept()
await super().connect()
except Exception as e:
print(f"Error in connect method: {e}")
await self.close()
async def disconnect(self, close_code):
try:
print("Disconnect method called")
await super().disconnect(close_code)
pass
except Exception as e:
print(f"Error in disconnect method: {e}")
async def receive(self, text_data):
print("Received data:", text_data)
text_data_json = json.loads(text_data)
msg_type = text_data_json.get('type', '') # Use get to provide a default value if 'type' is not present
message = text_data_json.get('message', '') # Use get to provide a default value if 'message' is not present
if msg_type == 'authenticate':
auth_token = message
self.user = await ChatService.get_user_from_token(auth_token)
self.room = await ChatService.get_chat_room_details(self.room_group_name)
logger.info(f"{self.user} Chat authentication Successful")
if self.user and self.room is not None:
self.room_type = await ChatService.get_room_type(self.room)
if self.room_type == 'group':
self.sender_status = 'online'
await ChatService.update_messages_read_status(self.user, self.room)
elif self.room_type == 'personal':
self.sender_status, self.receiver_status = await ChatService.get_user_status(
self.user, self.room, 'online')
user_status = {
'sender_status': self.sender_status,
'receiver_status': self.receiver_status
}
await self.send(text_data=json.dumps({
'type': 'chat_status',
'data': json.dumps(user_status),
}))
elif msg_type == 'text':
chat_message = await ChatService.save_messages(self.user, self.room, message, msg_type)
data = {
'message_data': {
'id': chat_message.id,
'message': chat_message.message,
'timestamp': chat_message.timestamp.isoformat(),
'message_status': chat_message.message_status,
},
'user_data': {
'first_name': chat_message.sender_user.first_name,
'last_name': chat_message.sender_user.last_name,
'profile_pic': chat_message.sender_user.profile_image.url or '',
},
'room_data': {
'room_name': chat_message.room.room_name,
},
}
if self.room_type == 'group':
await ChatService.update_messages_read_status(self.user, self.room)
data['message_data']['read_by'] = chat_message.read_by
elif self.room_type == 'personal':
self.sender_status, self.receiver_status = await ChatService.get_user_status(
self.user, self.room, 'online')
await self.send(text_data=json.dumps({
'type': 'chat_message',
'data': json.dumps(data),
}))
await ChatService.update_messages_status(chat_message, 'Sent')
I am getting same errors 'WebSocketProtocol' object has no attribute 'handshake_deferred' can anyone help me out to resolve this issue i have provided the code of consumers...?
@m-rakesh-kr take a break buddy
My error is because the version of daphne and channels does not match, or the version of daphne is too low to support multiple websocket connections