Django - ValueError: Socket has not been accepted, so cannot send over it

Django - ValueError: Socket has not been accepted, so cannot send over it

我开始使用 Django Channels 并创建了一个非常基本的消费者,但每当我尝试连接到该消费者时,我总是收到以下错误:

Exception inside application: Socket has not been accepted, so cannot send over it

这里是消费者:

class TestConsumer(WebsocketConsumer):
    def websocket_connect(self, event):
        self.send({
            'type': 'websocket.accept'
        })
       
        print('CONNECTED')

    def websocket_receive(self, event):
        data = event['text']
        print(data)

    def websocket_disconnect(self, event):
        print('DISCONNECTED!')

我在这里做错了什么?任何类型的建议都将受到赞赏。

而不是这个:

self.send({
    'type': 'websocket.accept'
})

试试这个:

self.accept({
    'type': 'websocket.accept'
})