mirror of
https://github.com/django/daphne.git
synced 2024-11-21 23:46:33 +03:00
Improve logging and fix websocket handoff factory
This commit is contained in:
parent
97f7673df9
commit
94cd1bbc53
|
@ -44,6 +44,7 @@ class WebRequest(http.Request):
|
||||||
self.setResponseCode(500)
|
self.setResponseCode(500)
|
||||||
self.finish()
|
self.finish()
|
||||||
# Port across transport
|
# Port across transport
|
||||||
|
protocol.set_main_factory(self.factory)
|
||||||
transport, self.transport = self.transport, None
|
transport, self.transport = self.transport, None
|
||||||
if isinstance(transport, ProtocolWrapper):
|
if isinstance(transport, ProtocolWrapper):
|
||||||
# i.e. TLS is a wrapping protocol
|
# i.e. TLS is a wrapping protocol
|
||||||
|
@ -59,7 +60,7 @@ class WebRequest(http.Request):
|
||||||
data += self.content.read()
|
data += self.content.read()
|
||||||
protocol.dataReceived(data)
|
protocol.dataReceived(data)
|
||||||
# Remove our HTTP reply channel association
|
# 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.factory.reply_protocols[self.reply_channel] = None
|
||||||
self.reply_channel = None
|
self.reply_channel = None
|
||||||
# Boring old HTTP.
|
# Boring old HTTP.
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
import logging
|
||||||
|
|
||||||
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
|
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
|
||||||
from django.http import parse_cookie
|
from django.http import parse_cookie
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class WebSocketProtocol(WebSocketServerProtocol):
|
class WebSocketProtocol(WebSocketServerProtocol):
|
||||||
"""
|
"""
|
||||||
|
@ -12,10 +15,8 @@ class WebSocketProtocol(WebSocketServerProtocol):
|
||||||
the websocket channels.
|
the websocket channels.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def set_main_factory(self, main_factory):
|
||||||
WebSocketServerProtocol.__init__(self, *args, **kwargs)
|
self.main_factory = main_factory
|
||||||
# Easy parent factory/channel layer link
|
|
||||||
self.main_factory = self.factory.main_factory
|
|
||||||
self.channel_layer = self.main_factory.channel_layer
|
self.channel_layer = self.main_factory.channel_layer
|
||||||
|
|
||||||
def onConnect(self, request):
|
def onConnect(self, request):
|
||||||
|
@ -33,9 +34,11 @@ class WebSocketProtocol(WebSocketServerProtocol):
|
||||||
# Tell main factory about it
|
# Tell main factory about it
|
||||||
self.main_factory.reply_protocols[self.reply_channel] = self
|
self.main_factory.reply_protocols[self.reply_channel] = self
|
||||||
# Send news that this channel is open
|
# 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)
|
self.channel_layer.send("websocket.connect", self.request_info)
|
||||||
|
|
||||||
def onMessage(self, payload, isBinary):
|
def onMessage(self, payload, isBinary):
|
||||||
|
logger.debug("WebSocket incoming packet on %s", self.reply_channel)
|
||||||
if isBinary:
|
if isBinary:
|
||||||
self.channel_layer.send("websocket.receive", {
|
self.channel_layer.send("websocket.receive", {
|
||||||
"reply_channel": self.reply_channel,
|
"reply_channel": self.reply_channel,
|
||||||
|
@ -51,6 +54,7 @@ class WebSocketProtocol(WebSocketServerProtocol):
|
||||||
"""
|
"""
|
||||||
Server-side channel message to send a message.
|
Server-side channel message to send a message.
|
||||||
"""
|
"""
|
||||||
|
logger.debug("Sent WebSocket packet to client for %s", self.reply_channel)
|
||||||
if binary:
|
if binary:
|
||||||
self.sendMessage(content, binary)
|
self.sendMessage(content, binary)
|
||||||
else:
|
else:
|
||||||
|
@ -63,6 +67,7 @@ class WebSocketProtocol(WebSocketServerProtocol):
|
||||||
self.sendClose()
|
self.sendClose()
|
||||||
|
|
||||||
def onClose(self, wasClean, code, reason):
|
def onClose(self, wasClean, code, reason):
|
||||||
|
logger.debug("WebSocket closed for %s", self.reply_channel)
|
||||||
if hasattr(self, "reply_channel"):
|
if hasattr(self, "reply_channel"):
|
||||||
del self.factory.reply_protocols[self.reply_channel]
|
del self.factory.reply_protocols[self.reply_channel]
|
||||||
self.channel_layer.send("websocket.disconnect", {
|
self.channel_layer.send("websocket.disconnect", {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user