Add channel to every consumer signature, update msg fmt docs

This commit is contained in:
Andrew Godwin 2015-06-08 13:35:27 -07:00
parent c2c1ffc5bd
commit 5a7af1e3af
4 changed files with 66 additions and 7 deletions

View File

@ -15,7 +15,7 @@ class UrlConsumer(object):
self.handler = BaseHandler() self.handler = BaseHandler()
self.handler.load_middleware() self.handler.load_middleware()
def __call__(self, **kwargs): def __call__(self, channel, **kwargs):
request = HttpRequest.channel_decode(kwargs) request = HttpRequest.channel_decode(kwargs)
try: try:
response = self.handler.get_response(request) response = self.handler.get_response(request)
@ -42,7 +42,7 @@ def view_consumer(channel_name, alias=None):
""" """
def inner(func): def inner(func):
@functools.wraps(func) @functools.wraps(func)
def consumer(**kwargs): def consumer(channel, **kwargs):
request = HttpRequest.channel_decode(kwargs) request = HttpRequest.channel_decode(kwargs)
response = func(request) response = func(request)
Channel(request.response_channel).send(**response.channel_encode()) Channel(request.response_channel).send(**response.channel_encode())

View File

@ -15,8 +15,9 @@ class BaseChannelBackend(object):
registry of consumers. registry of consumers.
""" """
def __init__(self): def __init__(self, expiry=60):
self.registry = ConsumerRegistry() self.registry = ConsumerRegistry()
self.expiry = expiry
def send(self, channel, message): def send(self, channel, message):
""" """

View File

@ -4,20 +4,78 @@ Message Standards
Some standardised message formats are used for common message types - they Some standardised message formats are used for common message types - they
are detailed below. are detailed below.
Note: All consumers also receive the channel name as the keyword argument
"channel", so there is no need for separate type information to let
multi-channel consumers distinguish.
HTTP Request HTTP Request
------------ ------------
Represents a full-fledged, single HTTP request coming in from a client. Represents a full-fledged, single HTTP request coming in from a client.
Contains the following keys: Contains the following keys:
* request: An encoded Django HTTP request * GET: List of (key, value) tuples of GET variables
* response_channel: The channel name to write responses to * POST: List of (key, value) tuples of POST variables
* COOKIES: Same as ``request.COOKIES``
* META: Same as ``request.META``
* path: Same as ``request.path``
* path_info: Same as ``request.path_info``
* method: Upper-cased HTTP method
* response_channel: Channel name to write response to
HTTP Response HTTP Response
------------- -------------
Sends a whole response to a client. Sends a whole response to a client.
Contains the following keys: Contains the following keys:
* response: An encoded Django HTTP response * content: String of content to send
* content_type: Mimetype of content
* status_code: Numerical HTTP status code
* headers: Dictionary of headers (key is header name, value is value)
HTTP Disconnect
---------------
Send when a client disconnects early, before the response has been sent.
Only sent by long-polling-capable HTTP interface servers.
Contains the same keys as HTTP Request.
WebSocket Connection
--------------------
Sent when a new WebSocket is connected.
Contains the following keys:
* GET: List of (key, value) tuples of GET variables
* COOKIES: Same as ``request.COOKIES``
* META: Same as ``request.META``
* path: Same as ``request.path``
* path_info: Same as ``request.path_info``
* send_channel: Channel name to send responses on
WebSocket Receive
-----------------
Sent when a datagram is received on the WebSocket.
Contains the same keys as WebSocket Connection, plus:
* content: String content of the datagram
WebSocket Close
---------------
Sent when the WebSocket is closed by either the client or the server.
Contains the same keys as WebSocket Connection, including send_channel,
though nothing should be sent on it.

View File

@ -16,4 +16,4 @@ class Worker(object):
while True: while True:
channel, message = self.channel_layer.receive_many(channels) channel, message = self.channel_layer.receive_many(channels)
consumer = self.channel_layer.registry.consumer_for_channel(channel) consumer = self.channel_layer.registry.consumer_for_channel(channel)
consumer(**message) consumer(channel=channel, **message)