Search code examples
pythondjangodjango-channels

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


I'm getting started to Django Channels and i created a very basic Consumer, but whenever i try to connect to that consumer, i keep getting the following error:

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

Here is the consumer:

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!')

What am i doing wrong here? Any kind of advice is appreciated.


Solution

  • Instead of this:

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

    Try This:

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