diff --git a/channels/adapters.py b/channels/adapters.py index bad2276..85ac5b6 100644 --- a/channels/adapters.py +++ b/channels/adapters.py @@ -15,7 +15,7 @@ class UrlConsumer(object): self.handler = BaseHandler() self.handler.load_middleware() - def __call__(self, **kwargs): + def __call__(self, channel, **kwargs): request = HttpRequest.channel_decode(kwargs) try: response = self.handler.get_response(request) @@ -42,7 +42,7 @@ def view_consumer(channel_name, alias=None): """ def inner(func): @functools.wraps(func) - def consumer(**kwargs): + def consumer(channel, **kwargs): request = HttpRequest.channel_decode(kwargs) response = func(request) Channel(request.response_channel).send(**response.channel_encode()) diff --git a/channels/backends/base.py b/channels/backends/base.py index a35fcdc..62866f0 100644 --- a/channels/backends/base.py +++ b/channels/backends/base.py @@ -15,8 +15,9 @@ class BaseChannelBackend(object): registry of consumers. """ - def __init__(self): + def __init__(self, expiry=60): self.registry = ConsumerRegistry() + self.expiry = expiry def send(self, channel, message): """ diff --git a/channels/docs/message-standards.rst b/channels/docs/message-standards.rst index 2f6d204..ef225cb 100644 --- a/channels/docs/message-standards.rst +++ b/channels/docs/message-standards.rst @@ -4,20 +4,78 @@ Message Standards Some standardised message formats are used for common message types - they 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 ------------ Represents a full-fledged, single HTTP request coming in from a client. + Contains the following keys: -* request: An encoded Django HTTP request -* response_channel: The channel name to write responses to +* GET: List of (key, value) tuples of GET variables +* 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 ------------- Sends a whole response to a client. + 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. diff --git a/channels/worker.py b/channels/worker.py index 9ce0606..47fa21a 100644 --- a/channels/worker.py +++ b/channels/worker.py @@ -16,4 +16,4 @@ class Worker(object): while True: channel, message = self.channel_layer.receive_many(channels) consumer = self.channel_layer.registry.consumer_for_channel(channel) - consumer(**message) + consumer(channel=channel, **message)