Django Channels Routing not working

Asked

Viewed 55 times

1

I wanted people to access my URL normally like before (install Channels) localhost/home/minha-slug and detected the socket messages within that route. I want to create Groups so I don’t keep sending messages to everyone on the site, but whoever 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 connects in chat normally, but it seems that does not send/receive message. Does anyone know where I’m going wrong or another way to work with groups/Christians?

Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.