I wanted people to access my URL as usual before (to install channels) localhost/home/minha-slug
and to detect socket messages within that route. I want to create Groups not to be sending messages to all of the site, but who is on the page.
My Routing:
home_routing = [
route("websocket.connect", ws_add, path=r'^/(?P<slug>[a-zA-Z0-9_]+)'),
route("websocket.receive", ws_message, path=r'^/(?P<slug>[a-zA-Z0-9_]+)'),
route("websocket.disconnect", ws_disconnect, path=r'^/(?P<slug>[a-zA-Z0-9_]+)'),
]
channel_routing = [
include(home_routing, path=r'^/home')
]
My consumer:
from channels import Group
import json
# Connected to websocket.connect
def ws_add(message, slug):
# Accept the connection
print(slug)
message.reply_channel.send({"accept": True})
# Add to the chat group
Group("room-%s" % slug).add(message.reply_channel)
# Connected to websocket.receive
def ws_message(message, slug):
print(slug)
Group("room-%s" % slug).send({
"text": "oi"
})
# Connected to websocket.disconnect
def ws_disconnect(message, slug):
Group("room-%s" % slug).discard(message.reply_channel)
Here it appears that it connects in the chat normally, but it seems that it does not send / receive message. Does anyone know where I'm going wrong or another way to work with groups / channels?
Thank you.