diff --git a/channels/generic/websockets.py b/channels/generic/websockets.py index b5d5cd7..d82c9fe 100644 --- a/channels/generic/websockets.py +++ b/channels/generic/websockets.py @@ -95,7 +95,7 @@ class WebsocketConsumer(BaseConsumer): """ message = {} if close: - message["close"] = True + message["close"] = close if text is not None: message["text"] = text elif bytes is not None: @@ -108,7 +108,7 @@ class WebsocketConsumer(BaseConsumer): def group_send(cls, name, text=None, bytes=None, close=False): message = {} if close: - message["close"] = True + message["close"] = close if text is not None: message["text"] = text elif bytes is not None: @@ -117,11 +117,11 @@ class WebsocketConsumer(BaseConsumer): raise ValueError("You must pass text or bytes") Group(name).send(message) - def close(self): + def close(self, status=True): """ Closes the WebSocket from the server end """ - self.message.reply_channel.send({"close": True}) + self.message.reply_channel.send({"close": status}) def raw_disconnect(self, message, **kwargs): """ @@ -134,7 +134,7 @@ class WebsocketConsumer(BaseConsumer): def disconnect(self, message, **kwargs): """ - Called when a WebSocket connection is opened. + Called when a WebSocket connection is closed. """ pass diff --git a/docs/asgi.rst b/docs/asgi.rst index 76b1f7b..914f061 100644 --- a/docs/asgi.rst +++ b/docs/asgi.rst @@ -841,14 +841,15 @@ message: * If ``bytes`` or ``text`` is present, accept the connection and send the data. * If ``accept`` is ``True``, accept the connection and do nothing else. -* If ``close`` is ``True``, reject the connection. If ``bytes`` or ``text`` is - also set, it should accept the connection, send the frame, then immediately - close the connection. +* If ``close`` is ``True`` or a positive integer, reject the connection. If + ``bytes`` or ``text`` is also set, it should accept the connection, send the + frame, then immediately close the connection. If received while the connection is established: * If ``bytes`` or ``text`` is present, send the data. -* If ``close`` is ``True``, close the connection after any send. +* If ``close`` is ``True`` or a positive integer, close the connection after + any send. * ``accept`` is ignored. Channel: ``websocket.send!`` @@ -859,8 +860,10 @@ Keys: * ``text``: Unicode string of frame content, if in text mode, or ``None``. -* ``close``: Boolean saying if the connection should be closed after data - is sent, if any. Optional, default ``False``. +* ``close``: Boolean indicating if the connection should be closed after + data is sent, if any. Alternatively, a positive integer specifying the + response code. The response code will be 1000 if you pass ``True``. + Optional, default ``False``. * ``accept``: Boolean saying if the connection should be accepted without sending a frame if it is in the handshake phase. diff --git a/docs/generics.rst b/docs/generics.rst index 09acf13..c7caeee 100644 --- a/docs/generics.rst +++ b/docs/generics.rst @@ -172,8 +172,8 @@ 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. +``self.close()`` is also provided to easily close the WebSocket from the +server end with an optional status code once you are done with it. .. _multiplexing: