Improve logging and fix websocket handoff factory

This commit is contained in:
Andrew Godwin 2016-01-02 17:54:15 -08:00
parent 97f7673df9
commit 94cd1bbc53
2 changed files with 11 additions and 5 deletions

View File

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

View File

@ -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", {