From d25bea15433dfccb84096dd9ceeb84af7339eee4 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 24 Feb 2016 00:41:31 +0000 Subject: [PATCH] Send `order` keys with WebSocket packets as per ASGI spec update --- daphne/ws_protocol.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/daphne/ws_protocol.py b/daphne/ws_protocol.py index f10508c..0a5d013 100755 --- a/daphne/ws_protocol.py +++ b/daphne/ws_protocol.py @@ -22,6 +22,7 @@ class WebSocketProtocol(WebSocketServerProtocol): def onConnect(self, request): self.request = request + self.packets_received = 0 try: # Sanitize and decode headers clean_headers = {} @@ -45,6 +46,7 @@ class WebSocketProtocol(WebSocketServerProtocol): "client": [self.transport.getPeer().host, self.transport.getPeer().port], "server": [self.transport.getHost().host, self.transport.getHost().port], "reply_channel": self.reply_channel, + "order": 0, } except: # Exceptions here are not displayed right, just 500. @@ -63,14 +65,17 @@ class WebSocketProtocol(WebSocketServerProtocol): def onMessage(self, payload, isBinary): logger.debug("WebSocket incoming packet on %s", self.reply_channel) + self.packets_received += 1 if isBinary: self.channel_layer.send("websocket.receive", { "reply_channel": self.reply_channel, + "order": self.packets_received, "bytes": payload, }) else: self.channel_layer.send("websocket.receive", { "reply_channel": self.reply_channel, + "order": self.packets_received, "text": payload.decode("utf8"), }) @@ -96,6 +101,7 @@ class WebSocketProtocol(WebSocketServerProtocol): del self.factory.reply_protocols[self.reply_channel] self.channel_layer.send("websocket.disconnect", { "reply_channel": self.reply_channel, + "order": self.packets_received + 1, }) self.factory.log_action("websocket", "disconnected", { "path": self.request.path,