mirror of
https://github.com/django/daphne.git
synced 2025-07-30 00:49:44 +03:00
Improve docs consistency on query strings (#681)
Change to room_name parameter and erase references to query strings throughout the documentation tutorial.
This commit is contained in:
parent
63b79db352
commit
1551cb0563
|
@ -364,7 +364,7 @@ Persisting Data
|
||||||
Echoing messages is a nice simple example, but it's ignoring the real
|
Echoing messages is a nice simple example, but it's ignoring the real
|
||||||
need for a system like this - persistent state for connections.
|
need for a system like this - persistent state for connections.
|
||||||
Let's consider a basic chat site where a user requests a chat room upon initial
|
Let's consider a basic chat site where a user requests a chat room upon initial
|
||||||
connection, as part of the query string (e.g. ``wss://host/websocket?room=abc``).
|
connection, as part of the URL path (e.g. ``wss://host/rooms/room-name``).
|
||||||
|
|
||||||
The ``reply_channel`` attribute you've seen before is our unique pointer to the
|
The ``reply_channel`` attribute you've seen before is our unique pointer to the
|
||||||
open WebSocket - because it varies between different clients, it's how we can
|
open WebSocket - because it varies between different clients, it's how we can
|
||||||
|
@ -390,26 +390,24 @@ name in the path of your WebSocket request (we'll ignore auth for now - that's n
|
||||||
|
|
||||||
# Connected to websocket.connect
|
# Connected to websocket.connect
|
||||||
@channel_session
|
@channel_session
|
||||||
def ws_connect(message):
|
def ws_connect(message, room_name):
|
||||||
# Accept connection
|
# Accept connection
|
||||||
message.reply_channel.send({"accept": True})
|
message.reply_channel.send({"accept": True})
|
||||||
# Work out room name from path (ignore slashes)
|
|
||||||
room = message.content['path'].strip("/")
|
|
||||||
# Save room in session and add us to the group
|
# Save room in session and add us to the group
|
||||||
message.channel_session['room'] = room
|
message.channel_session['room_name'] = room_name
|
||||||
Group("chat-%s" % room).add(message.reply_channel)
|
Group("chat-%s" % room).add(message.reply_channel)
|
||||||
|
|
||||||
# Connected to websocket.receive
|
# Connected to websocket.receive
|
||||||
@channel_session
|
@channel_session
|
||||||
def ws_message(message):
|
def ws_message(message, room_name):
|
||||||
Group("chat-%s" % message.channel_session['room']).send({
|
Group("chat-%s" % message.channel_session['room_name']).send({
|
||||||
"text": message['text'],
|
"text": message['text'],
|
||||||
})
|
})
|
||||||
|
|
||||||
# Connected to websocket.disconnect
|
# Connected to websocket.disconnect
|
||||||
@channel_session
|
@channel_session
|
||||||
def ws_disconnect(message):
|
def ws_disconnect(message, room_name):
|
||||||
Group("chat-%s" % message.channel_session['room']).discard(message.reply_channel)
|
Group("chat-%s" % room_name).discard(message.reply_channel)
|
||||||
|
|
||||||
Update ``routing.py`` as well::
|
Update ``routing.py`` as well::
|
||||||
|
|
||||||
|
@ -591,7 +589,7 @@ routing our chat from above::
|
||||||
]
|
]
|
||||||
|
|
||||||
chat_routing = [
|
chat_routing = [
|
||||||
route("websocket.connect", chat_connect, path=r"^/(?P<room>[a-zA-Z0-9_]+)/$"),
|
route("websocket.connect", chat_connect, path=r"^/(?P<room_name>[a-zA-Z0-9_]+)/$"),
|
||||||
route("websocket.disconnect", chat_disconnect),
|
route("websocket.disconnect", chat_disconnect),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -616,13 +614,13 @@ consumer above to use a room based on URL rather than username::
|
||||||
|
|
||||||
# Connected to websocket.connect
|
# Connected to websocket.connect
|
||||||
@channel_session_user_from_http
|
@channel_session_user_from_http
|
||||||
def ws_add(message, room):
|
def ws_add(message, room_name):
|
||||||
# Add them to the right group
|
# Add them to the right group
|
||||||
Group("chat-%s" % room).add(message.reply_channel)
|
Group("chat-%s" % room_name).add(message.reply_channel)
|
||||||
# Accept the connection request
|
# Accept the connection request
|
||||||
message.reply_channel.send({"accept": True})
|
message.reply_channel.send({"accept": True})
|
||||||
|
|
||||||
In the next section, we'll change to sending the ``room`` as a part of the
|
In the next section, we'll change to sending the ``room_name`` as a part of the
|
||||||
WebSocket message - which you might do if you had a multiplexing client -
|
WebSocket message - which you might do if you had a multiplexing client -
|
||||||
but you could use routing there as well.
|
but you could use routing there as well.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user