Django 通道,AttributeError

Django channels, AttributeError

我正在尝试学习如何使用通道 2 通过 websocket 发送消息。

我只是想在客户端连接时发送一条简单的消息,但我一直收到属性错误:

class CameraOnlineConsumer(JsonWebsocketConsumer):
    def connect(self):
        self.accept()
        return self.send({
            "type": "websocket.accept",
            "send": {'a': "Hi"}
        })

加注:

self.sendMessage(content.encode("utf8"), binary)
AttributeError: 'dict' object has no attribute 'encode'
WebSocket DISCONNECT /ws/camera_online/connect [127.0.0.1:36006]

更改为:

 return self.send({
            "type": "websocket.accept",
            "send": "Hi"
        })

例如returns同样的错误。

Try convert it to json like this ..


 import json    
    class CameraOnlineConsumer(JsonWebsocketConsumer):
            def connect(self):
                self.accept()
                return self.send(json.dumps({
                    "type": "websocket.accept",
                    "send": {'a': "Hi"}
                }))