Django Channels Group.send没有在python控制台中工作?

时间:2017-05-28 05:03:25

标签: python django django-channels

我在python控制台中尝试了Group(groupname).send它似乎不起作用。这是为什么?

这是我的consumer.py安排:

def ws_connect(message):
    message.reply_channel.send({"accept": True})
    Group(secure_group).add(message.reply_channel)


def ws_receive(message):
    # Nothing to do here
    Group(secure_group).send({
        "text": "Received {}".format(message.content['text'])
    })


def ws_disconnect(message):
    Group(secure_group).discard(message.reply_channel)

路由:

from channels.routing import route
from App.consumers import (
    ws_connect,
    ws_receive,
    ws_disconnect
)

channel_routing = [
    route("websocket.connect", ws_connect),
    route("websocket.receive", ws_receive),
    route("websocket.disconnect", ws_disconnect),
]

终端命令:

from channels import Group
#import secure_group here

Group(secure_group).send({ "text": "Tester" })

我的所有客户都没有收到过该文本。

CHANNEL_LAYERS config:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgiref.inmemory.ChannelLayer",
        "ROUTING": "App.routing.channel_routing",
    },
}

1 个答案:

答案 0 :(得分:1)

Inmemory频道图层不支持cross-process communication。您无法在其他终端中执行群组发送。尝试使用Redis后端,您可以发送消息。

来自文档In-Memory