From 94cd1bbc53fb670f80d9bca766c630f425a36a31 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 2 Jan 2016 17:54:15 -0800 Subject: [PATCH] Improve logging and fix websocket handoff factory --- daphne/http_protocol.py | 3 ++- daphne/ws_protocol.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 4a87333..795af41 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -44,6 +44,7 @@ class WebRequest(http.Request): self.setResponseCode(500) self.finish() # Port across transport + protocol.set_main_factory(self.factory) transport, self.transport = self.transport, None if isinstance(transport, ProtocolWrapper): # i.e. TLS is a wrapping protocol @@ -59,7 +60,7 @@ class WebRequest(http.Request): data += self.content.read() protocol.dataReceived(data) # Remove our HTTP reply channel association - logging.debug("Upgraded connection %s to WebSocket", self.reply_channel) + logging.debug("Upgraded connection %s to WebSocket %s", self.reply_channel, protocol.reply_channel) self.factory.reply_protocols[self.reply_channel] = None self.reply_channel = None # Boring old HTTP. diff --git a/daphne/ws_protocol.py b/daphne/ws_protocol.py index 2915b88..a962675 100755 --- a/daphne/ws_protocol.py +++ b/daphne/ws_protocol.py @@ -1,10 +1,13 @@ from __future__ import unicode_literals import time +import logging from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory from django.http import parse_cookie +logger = logging.getLogger(__name__) + class WebSocketProtocol(WebSocketServerProtocol): """ @@ -12,10 +15,8 @@ class WebSocketProtocol(WebSocketServerProtocol): the websocket channels. """ - def __init__(self, *args, **kwargs): - WebSocketServerProtocol.__init__(self, *args, **kwargs) - # Easy parent factory/channel layer link - self.main_factory = self.factory.main_factory + def set_main_factory(self, main_factory): + self.main_factory = main_factory self.channel_layer = self.main_factory.channel_layer def onConnect(self, request): @@ -33,9 +34,11 @@ class WebSocketProtocol(WebSocketServerProtocol): # Tell main factory about it self.main_factory.reply_protocols[self.reply_channel] = self # Send news that this channel is open + logger.debug("WebSocket open for %s", self.reply_channel) self.channel_layer.send("websocket.connect", self.request_info) def onMessage(self, payload, isBinary): + logger.debug("WebSocket incoming packet on %s", self.reply_channel) if isBinary: self.channel_layer.send("websocket.receive", { "reply_channel": self.reply_channel, @@ -51,6 +54,7 @@ class WebSocketProtocol(WebSocketServerProtocol): """ Server-side channel message to send a message. """ + logger.debug("Sent WebSocket packet to client for %s", self.reply_channel) if binary: self.sendMessage(content, binary) else: @@ -63,6 +67,7 @@ class WebSocketProtocol(WebSocketServerProtocol): self.sendClose() def onClose(self, wasClean, code, reason): + logger.debug("WebSocket closed for %s", self.reply_channel) if hasattr(self, "reply_channel"): del self.factory.reply_protocols[self.reply_channel] self.channel_layer.send("websocket.disconnect", {