Need to hook each of these channels up in routing.py
:
from . import consumers
channel_routing = {
'websocket.connect': consumers.ws_connect,
'websocket.receive': consumers.ws_receive,
'websocket.disconnect': consumers.ws_disconnect,
}
Pretty simple stuff: just connect each channel to an appropriate function. Now, let’s look at those functions. By convention we’ll put these functions in aconsumers.py
(but, like views, these could be anywhere.)
Let’s look at ws_connect
first:
from channels import Group
from channels.sessions import channel_session
from .models import Room
@channel_session
def ws_connect(message):
prefix, label = message['path'].strip('/').split('/')
room = Room.objects.get(label=label)
Group('chat-' + label).add(message.reply_channel)
message.channel_session['room'] = room.label
___
prefix, label = message['path'].strip('/').split('/').
from channels.urls import parameters # I know this doesn't exist
from. import consumers
channel_routing = {
parameters('websocket.connect', consumers.ws_connect, path=r'chat/(?P<label>\w+)',
'websocket.receive': consumers.ws_receive,
'websocket.disconnect': consumers.ws_disconnect,
}
from channels import Group from channels.sessions import channel_session from .models import Room @channel_session def ws_connect(message, *args, **kwrags): label = kwargs['label'] room = Room.objects.get(label=label) Group('chat-' + label).add(message.reply_channel) message.channel_session['room'] = room.label
Thoughts?
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/eb8bb34d-7c07-4443-960e-fe20a6b2eb90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.