Javascript Django通道websocket未接收发送的数据
我目前正在学习如何使用Django通道进行websocket连接,到目前为止,我已经让websocket实际连接,但当我发送数据时,它不会接收到数据 这是我的消费者Javascript Django通道websocket未接收发送的数据,javascript,django,websocket,django-channels,Javascript,Django,Websocket,Django Channels,我目前正在学习如何使用Django通道进行websocket连接,到目前为止,我已经让websocket实际连接,但当我发送数据时,它不会接收到数据 这是我的消费者 class WSConsumer(AsyncConsumer): async def websocket_connect(self, event): print('connected', event) await self.send({ 'type': 'websock
class WSConsumer(AsyncConsumer):
async def websocket_connect(self, event):
print('connected', event)
await self.send({
'type': 'websocket.accept'
})
await self.send({
'type': 'websocket.send',
'message': 'TEST'
})
print('sent')
async def websocket_disconnect(self, event):
print('disconnected', event)
async def websocket_receive(self, event):
print('received', event))
这是我前端的javascript
const roomName = JSON.parse(document.getElementById('room-name').textContent);
const WSocket = new WebSocket(
'ws://'
+ window.location.host
+ '/ws/room/'
+ roomName
+ '/'
);
WSocket.onopen = function(e) {
console.log('websocket has connected');
};
WSocket.onmessage = function(e) {
console.log(e);
};
WSocket.onclose = function(e) {
console.error('websocket closed unexpectedly');
};
Woops才意识到我是个站在男人的立场上的傻瓜 在下面我最初编写的代码中
await self.send({
'type': 'websocket.send',
'message': 'TEST'
})
应该是什么时候
await self.send({
'type': 'websocket.send',
'text': 'TEST'
})
它应该是“文本”类型而不是“消息”