django 通道 websocket 在握手期间关闭
django channels websocket closes during handshake
我正在尝试使用 channels
readthedocs https://channels.readthedocs.io/en/latest/tutorial/index.html
中的教程将一个简单的聊天室集成到我当前的网站项目中
当我尝试加载应该自动连接到全球聊天的网站主页时,我得到了这个
后端控制台
HTTP GET / 200 [0.01, 127.0.0.1:58286]
HTTP GET /static/chatroom/base.css 304 [0.00, 127.0.0.1:58286]
HTTP GET /static/chatroom/images/profiles/anonymous.jpg 304 [0.00, 127.0.0.1:58288]
HTTP GET /static/chatroom/images/background.jpg 304 [0.00, 127.0.0.1:58288]
WebSocket HANDSHAKING /ws/chat/global/ [127.0.0.1:58290]
WebSocket DISCONNECT /ws/chat/global/ [127.0.0.1:58282]
javascript 控制台
Firefox can’t establish a connection to the server at ws://127.0.0.1:8000/ws/chat/global/. 127.0.0.1:8000:59:23
Socket error: [object WebSocket] 127.0.0.1:8000:69:17
Chat socket closed unexpectedly: 1006
我不太确定应该为我的示例提供什么代码,但这是我的消费者代码和我的设置
consumers.py
# chat/consumers.py
import json
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
class ChatConsumer(WebsocketConsumer):
async def connect(self):
self.room_name = 'global'
self.room_group_name = 'chat_%s' % self.room_name
# Join room group
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
# Receive message from WebSocket
async def receive(self, text_data=None, bytes_data=None):
text_data_json = json.loads(text_data)
message = text_data_json['message']
# Send message to room group
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
# Receive message from room group
async def chat_message(self, event):
message = event['message']
# Send message to WebSocket
await self.send(text_data=json.dumps({
'message': message
}))
settings.py(截断)
# chatroom settings
# https://channels.readthedocs.io/en/latest/tutorial/part_2.html
ASGI_APPLICATION = 'mysite.routing.application'
CHAT_PORT = 7580
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": ['redis://localhost:{}'.format(CHAT_PORT)]
},
}
}
我尝试查看其他帖子,但它们似乎都使用过时的版本,我可以得到更新的答案吗?
您使用的是同步 WebSocketConsumer
而您已将代码重写为异步。因此你应该写:
class ChatConsumer(AsyncWebsocketConsumer):
将您的 consumers.py
与 the tutorial 中的进行比较。
我正在尝试使用 channels
readthedocs https://channels.readthedocs.io/en/latest/tutorial/index.html
当我尝试加载应该自动连接到全球聊天的网站主页时,我得到了这个
后端控制台
HTTP GET / 200 [0.01, 127.0.0.1:58286]
HTTP GET /static/chatroom/base.css 304 [0.00, 127.0.0.1:58286]
HTTP GET /static/chatroom/images/profiles/anonymous.jpg 304 [0.00, 127.0.0.1:58288]
HTTP GET /static/chatroom/images/background.jpg 304 [0.00, 127.0.0.1:58288]
WebSocket HANDSHAKING /ws/chat/global/ [127.0.0.1:58290]
WebSocket DISCONNECT /ws/chat/global/ [127.0.0.1:58282]
javascript 控制台
Firefox can’t establish a connection to the server at ws://127.0.0.1:8000/ws/chat/global/. 127.0.0.1:8000:59:23
Socket error: [object WebSocket] 127.0.0.1:8000:69:17
Chat socket closed unexpectedly: 1006
我不太确定应该为我的示例提供什么代码,但这是我的消费者代码和我的设置
consumers.py
# chat/consumers.py
import json
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
class ChatConsumer(WebsocketConsumer):
async def connect(self):
self.room_name = 'global'
self.room_group_name = 'chat_%s' % self.room_name
# Join room group
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
# Receive message from WebSocket
async def receive(self, text_data=None, bytes_data=None):
text_data_json = json.loads(text_data)
message = text_data_json['message']
# Send message to room group
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'chat_message',
'message': message
}
)
# Receive message from room group
async def chat_message(self, event):
message = event['message']
# Send message to WebSocket
await self.send(text_data=json.dumps({
'message': message
}))
settings.py(截断)
# chatroom settings
# https://channels.readthedocs.io/en/latest/tutorial/part_2.html
ASGI_APPLICATION = 'mysite.routing.application'
CHAT_PORT = 7580
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": ['redis://localhost:{}'.format(CHAT_PORT)]
},
}
}
我尝试查看其他帖子,但它们似乎都使用过时的版本,我可以得到更新的答案吗?
您使用的是同步 WebSocketConsumer
而您已将代码重写为异步。因此你应该写:
class ChatConsumer(AsyncWebsocketConsumer):
将您的 consumers.py
与 the tutorial 中的进行比较。