Django频道不发送回复消息

时间:2018-01-31 19:02:28

标签: python django sockets rabbitmq channels

让我先说一下:我是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]

1 个答案:

答案 0 :(得分:0)

事实证明,这可能是因为RabbitMQ并不是为了像这样在飞行中创建队列。所以它太慢导致websocket超时。我切换到Redis通道层,现在看起来效果很好!