1π
i got same problem when i try to disconnect the client from client side (by using Postman application ) , and i found the following solution , by raise a StopConsumer() Exception inside websoket disconnect method, kindly have a look at the following official DOCs link https://channels.readthedocs.io/en/stable/topics/consumers.html#closing-consumers
and here is a simple example :
from channels.consumer import SyncConsumer
from channels.exceptions import StopConsumer
class MySyncConsumer(SyncConsumer):
def websocket_connect(self, event):
print('websocket connected1...', event)
self.send({
'type': 'websocket.accept'
})
print('websocket connected2...', event)
def websocket_receive(self, event):
print('websocket recived message...', event)
def websocket_disconnect(self, event):
print('websocket disconnected...', event)
raise StopConsumer()
i donβt know if my case same your case but i hope this helpful .
π€K.A
0π
In case anyone is using syncWebsocketConsumer
the syntax is the same as @K.A.βs answer, and resolved my issue as well. Thanks @K.A. for what should be the accepted answer.
Here is the async version of what fixed my issue.
async def disconnect(self, close_code):
print(close_code)
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
raise StopConsumer()
π€MattG
- Django/Python Runtime Error: Maximum recursion depth exceeded
- Is there any list of blog engines, written in Django?
- Python venv not creating virtual environment
- Why does Django use tuples for settings and not lists?
- How do I rebuild my django-mptt tree?
Source:stackexchange.com