Improve docs. (#589)

This commit is contained in:
Artem Malyshev 2017-04-05 13:41:30 +03:00 committed by Andrew Godwin
parent 627b97c317
commit c1f801a20e

View File

@ -625,6 +625,8 @@ have a ChatMessage model with ``message`` and ``room`` fields::
# Save room in session and add us to the group
message.channel_session['room'] = room
Group("chat-%s" % room).add(message.reply_channel)
# Accept the connection request
message.reply_channel.send({"accept": True})
# Connected to websocket.receive
@channel_session
@ -640,6 +642,19 @@ have a ChatMessage model with ``message`` and ``room`` fields::
def ws_disconnect(message):
Group("chat-%s" % message.channel_session['room']).discard(message.reply_channel)
Update ``routing.py`` as well::
# in routing.py
from channels.routing import route
from myapp.consumers import ws_connect, ws_message, ws_disconnect, msg_consumer
channel_routing = [
route("websocket.connect", ws_connect),
route("websocket.receive", ws_message),
route("websocket.disconnect", ws_disconnect),
route("chat-messages", msg_consumer),
]
Note that we could add messages onto the ``chat-messages`` channel from anywhere;
inside a View, inside another model's ``post_save`` signal, inside a management
command run via ``cron``. If we wanted to write a bot, too, we could put its