2

I'm trying to learn how to send messages over a websocket using channels 2.

I'm trying just to send a simple message when a client connects but I keep getting Attribute Errors:

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

raises:

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

changing to:

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

for example, returns the same error.

1 Answer 1

2
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"}
                }))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.