[英]How to send data client to server django channel
Hi all I'm trying send data js to django with websocket but not working Could you help me where am I doing wrong? 大家好,我正在尝试使用websocket将数据js发送到django,但无法正常工作。您能帮我哪里做错了吗?
ps: I was able to send data from the server to the client ps:我能够将数据从服务器发送到客户端
error: 错误:
raise ValueError("No text section for incoming WebSocket frame!")
引发ValueError(“传入的WebSocket框架没有文本部分!”)
No text section for incoming WebSocket frame!
传入的WebSocket框架没有文本部分!
WebSocket DISCONNECT /ws/ [127.0.0.1:64282]
WebSocket断开连接/ ws / [127.0.0.1:64282]
js js
document.addEventListener('DOMContentLoaded', function () {
let webSocketBridge = new WebSocket("ws://127.0.0.1:8000/ws/");
webSocketBridge.onopen = function(action) {
console.log(action);
webSocketBridge.send(JSON.stringify({
"id": "client1"
}));
};
webSocketBridge.onmessage = function(event) {
let message = event.data;
as += message + "\r\n";
$('#messages').html(as);
};
consumer.py 消费者
import asyncio
from channels.generic.websocket import AsyncJsonWebsocketConsumer
class TickTockConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
while 1:
await asyncio.sleep(0.1)
await self.send_json("tick")
await asyncio.sleep(0.1)
await self.send_json(".......tock")
await self.receive()
I found it 我找到了
class TickTockConsumer(WebsocketConsumer):
def connect(self):
self.accept()
self.send("tick")
self.send(".......tock")
def receive(self, text_data):
print(text_data)
or with async json 或使用异步json
class TickTockConsumer(AsyncJsonWebsocketConsumer):
async def connect(self):
await self.accept()
await self.send("tick")
await self.send(".......tock")
async def receive(self, text_data):
print(text_data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.