This commit is contained in:
aqu415
2022-02-15 21:06:02 +08:00
parent 2782fda765
commit 2c5db12e04
2 changed files with 9 additions and 3 deletions
+1
View File
@@ -24,6 +24,7 @@ class ChatConsumer(WebsocketConsumer):
# Receive message from WebSocket # Receive message from WebSocket
def receive(self, text_data): def receive(self, text_data):
# Send message to room group # Send message to room group
print(text_data)
async_to_sync(self.channel_layer.group_send)( async_to_sync(self.channel_layer.group_send)(
self.room_group_name, self.room_group_name,
{ {
+8 -3
View File
@@ -72,11 +72,13 @@
ws.onopen = function () { ws.onopen = function () {
//当WebSocket创建成功时,触发onopen事件 //当WebSocket创建成功时,触发onopen事件
console.log("open"); console.log("open");
ws.send("hello"); //将消息发送到服务端 ws.send(`{"h":"w"}`); //将消息发送到服务端
} }
ws.onmessage = function (e) { ws.onmessage = function (e) {
//当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据 //当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据
console.log(e.data); // console.log(e.data);
console.log('onmessage:');
console.log(JSON.parse(e.data));
} }
ws.onclose = function (e) { ws.onclose = function (e) {
//当客户端收到服务端发送的关闭连接请求时,触发onclose事件 //当客户端收到服务端发送的关闭连接请求时,触发onclose事件
@@ -142,7 +144,10 @@
param.data = data.join(';') param.data = data.join(';')
param.node = body.node param.node = body.node
param.coop_mind_id = this.coop_mind_id param.coop_mind_id = this.coop_mind_id
console.log(param)
// 序列化数据,处理特殊字符
const msg = Jm.util.json.json2string(param).replaceAll('\\n', '<br/>')
this._send_ws_message(msg)
} }
}, },
_send_ws_message: function (msg) { _send_ws_message: function (msg) {