Update bad example code

This commit is contained in:
Andrew Godwin 2015-09-02 00:09:01 -07:00
parent feb3017c0e
commit 15b54b4887

View File

@ -219,26 +219,26 @@ Fortunately, because Channels has standardised WebSocket event
authentication, as well as using Django's session framework (which authentication authentication, as well as using Django's session framework (which authentication
relies on). relies on).
All we need to do is add the ``websocket_auth`` decorator to our views, All we need to do is add the ``django_http_auth`` decorator to our views,
and we'll get extra ``session`` and ``user`` keyword arguments we can use; and we'll get extra ``session`` and ``user`` keyword arguments we can use;
let's make one where users can only chat to people with the same first letter let's make one where users can only chat to people with the same first letter
of their username:: of their username::
from channels import Channel from channels import Channel, Group
from channels.decorators import consumer, websocket_auth from channels.decorators import consumer, django_http_auth
@consumer("django.websocket.connect", "django.websocket.keepalive") @consumer("django.websocket.connect", "django.websocket.keepalive")
@websocket_auth @django_http_auth
def ws_add(channel, send_channel, user, **kwargs): def ws_add(channel, send_channel, user, **kwargs):
Group("chat-%s" % user.username[0]).add(send_channel) Group("chat-%s" % user.username[0]).add(send_channel)
@consumer("django.websocket.receive") @consumer("django.websocket.receive")
@websocket_auth @django_http_auth
def ws_message(channel, send_channel, content, user, **kwargs): def ws_message(channel, send_channel, content, user, **kwargs):
Group("chat-%s" % user.username[0]).send(content=content) Group("chat-%s" % user.username[0]).send(content=content)
@consumer("django.websocket.disconnect") @consumer("django.websocket.disconnect")
@websocket_auth @django_http_auth
def ws_disconnect(channel, send_channel, user, **kwargs): def ws_disconnect(channel, send_channel, user, **kwargs):
Group("chat-%s" % user.username[0]).discard(send_channel) Group("chat-%s" % user.username[0]).discard(send_channel)
@ -266,7 +266,7 @@ some other data store - sound familiar? This is what Django's session framework
does for HTTP requests, only there it uses cookies as the lookup key rather does for HTTP requests, only there it uses cookies as the lookup key rather
than the ``send_channel``. than the ``send_channel``.
Now, as you saw above, you can use the ``websocket_auth`` decorator to get Now, as you saw above, you can use the ``django_http_auth`` decorator to get
both a ``user`` and a ``session`` variable in your message arguments - and, both a ``user`` and a ``session`` variable in your message arguments - and,
indeed, there is a ``websocket_session`` decorator that will just give you indeed, there is a ``websocket_session`` decorator that will just give you
the ``session`` attribute. the ``session`` attribute.