Django Channels 不发送回复消息

Django Channels not sending reply message

让我先说一句:我是 RabbitMQ 及其相关概念的初学者。不过我想我开始明白了。

我目前在使用 Django 频道时遇到问题。我有一个使用 'asgiref.inmemory.ChannelLayer' 的设置,我目前正在切换到 'asgi_rabbitmq.RabbitmqChannelLayer' 这一切似乎都工作正常,直到我尝试从浏览器连接到 websocket。有一段时间(7 或 8 秒)没有任何反应,然后突然 websocket.receive 被触发并且 websocket 断开连接。

我认为这是因为回复频道没有收到我的接受消息。(或者可能为时已晚)。

路由:

lobby_routing = [
    route('websocket.connect', ws_test_add, path=r"^/testadd/$"),
]

消费者:

def ws_test_add(message):
    print "TEST"
    message.reply_channel.send({'accept': True})
    Group("testadd").add(message.reply_channel)

设置:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_rabbitmq.RabbitmqChannelLayer",
        "ROUTING": 'CouchGames_Backend.routing.lobby_routing',
        "CONFIG": {
            'url': 'amqp://guest:guest@localhost:5672/%2F',
        },
    },
}

日志:

2018-01-31 20:00:21,141 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-01-31 20:00:21,144 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-01-31 20:00:21,157 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-01-31 20:00:21,157 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive
2018-01-31 20:00:21,163 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-01-31 20:00:21,164 - INFO - server - Using busy-loop synchronous mode on channel layer
2018-01-31 20:00:21,164 - INFO - server - Listening on endpoint tcp:port=8000:interface=127.0.0.1
[2018/01/31 20:00:41] WebSocket HANDSHAKING /testadd/ [127.0.0.1:31404]
TEST
[2018/01/31 20:00:41] WebSocket DISCONNECT /testadd/ [127.0.0.1:31404]

事实证明,这可能是因为 RabbitMQ 并不意味着像那样动态创建队列。所以它太慢导致 websocket 超时。我切换到 Redis 通道层,现在它似乎运行良好!