Add closing response codes (#426)

Added both to spec and implementation. Regards #414.
This commit is contained in:
Tobias Kunze 2016-11-05 13:39:44 +01:00 committed by Andrew Godwin
parent f4f45dbb9f
commit 82f7ff21df
3 changed files with 16 additions and 13 deletions

View File

@ -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

View File

@ -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.

View File

@ -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: