mirror of
https://github.com/django/daphne.git
synced 2024-11-28 18:53:44 +03:00
Merge pull request #58 from rixx/✨
Respond with a code when closing a connection
This commit is contained in:
commit
a672da7b21
|
@ -275,7 +275,6 @@ class WebRequest(http.Request):
|
|||
})
|
||||
|
||||
|
||||
|
||||
class HTTPProtocol(http.HTTPChannel):
|
||||
|
||||
requestFactory = WebRequest
|
||||
|
@ -335,11 +334,13 @@ class HTTPFactory(http.HTTPFactory):
|
|||
protocol.serverSend(message["bytes"], True)
|
||||
if message.get("text", None):
|
||||
protocol.serverSend(message["text"], False)
|
||||
if message.get("close", False):
|
||||
|
||||
closing_code = message.get("close", False)
|
||||
if closing_code:
|
||||
if protocol.state == protocol.STATE_CONNECTING:
|
||||
protocol.serverReject()
|
||||
else:
|
||||
protocol.serverClose()
|
||||
protocol.serverClose(code=closing_code)
|
||||
else:
|
||||
raise ValueError("Cannot dispatch message on channel %r" % channel)
|
||||
|
||||
|
|
|
@ -105,7 +105,7 @@ class WebSocketProtocol(WebSocketServerProtocol):
|
|||
# so drop the connection.
|
||||
self.muted = True
|
||||
logger.warn("WebSocket force closed for %s due to connect backpressure", self.reply_channel)
|
||||
# Send code 1013 "try again later" with close.
|
||||
# Send code 503 "Service Unavailable" with close.
|
||||
raise ConnectionDeny(code=503, reason="Connection queue at capacity")
|
||||
else:
|
||||
self.factory.log_action("websocket", "connecting", {
|
||||
|
@ -198,11 +198,12 @@ class WebSocketProtocol(WebSocketServerProtocol):
|
|||
else:
|
||||
self.sendMessage(content.encode("utf8"), binary)
|
||||
|
||||
def serverClose(self):
|
||||
def serverClose(self, code=True):
|
||||
"""
|
||||
Server-side channel message to close the socket
|
||||
"""
|
||||
self.sendClose()
|
||||
code = 1000 if code is True else code
|
||||
self.sendClose(code=code)
|
||||
|
||||
def onClose(self, wasClean, code, reason):
|
||||
self.cleanup()
|
||||
|
|
Loading…
Reference in New Issue
Block a user