From af606ff89505dca33d9716f14342544c4cbbb80d Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 13 Jul 2016 17:19:39 -0700 Subject: [PATCH] Fixed #244: .close() on Websocket generic consumers --- channels/generic/websockets.py | 6 ++++++ docs/generics.rst | 3 +++ 2 files changed, 9 insertions(+) diff --git a/channels/generic/websockets.py b/channels/generic/websockets.py index f07ee25..38504f0 100644 --- a/channels/generic/websockets.py +++ b/channels/generic/websockets.py @@ -106,6 +106,12 @@ class WebsocketConsumer(BaseConsumer): else: raise ValueError("You must pass text or bytes") + def close(self): + """ + Closes the WebSocket from the server end + """ + self.message.reply_channel.send({"close": True}) + def raw_disconnect(self, message, **kwargs): """ Called when a WebSocket connection is closed. Base level so you don't diff --git a/docs/generics.rst b/docs/generics.rst index 1272f22..326f0c0 100644 --- a/docs/generics.rst +++ b/docs/generics.rst @@ -172,6 +172,9 @@ Note that this subclass still can't intercept ``Group.send()`` calls to make them into JSON automatically, but it does provide ``self.group_send(name, content)`` that will do this for you if you call it explicitly. +``self.close()`` is also provided to easily close the WebSocket from the server +end once you are done with it. + Sessions and Users ------------------