Django渠道集团发送停止工作

时间:2017-07-19 12:11:32

标签: django websocket django-views django-channels

我正在订购系统并使用django-channels向管理员发送有关已下订单的通知,并且在没有通知后它只能完成1-2个订单。

consumers.py

from channels import Group
from channels.sessions import channel_session   


@channel_session
def ws_connect(message):
    print "Connect"
    message.reply_channel.send({"accept": True})
    Group('admin-channel', channel_layer=message.channel_layer).add(message.reply_channel)  


@channel_session
def ws_disconnect(message):
    print "Disconnect"
    Group('admin-channel', channel_layer=message.channel_layer).discard(message.reply_channel)

views.py

def view(request):
    # some order realted processing
    message = {
        "status": "Success",
        "order-id": order.id
    }

    Group('admin-channel').send({
        "text": json.dumps(message)
    })

chat.js

$(function () {
    // When we're using HTTPS, use WSS too.
    var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
    var chatsock = new ReconnectingWebSocket(ws_scheme + '://' + window.location.host + "/order");  

    chatsock.onmessage = function (message) {
        var data = JSON.parse(message.data);
        console.log(data);
    };
});

settings.py

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
        "ROUTING": "cashier.routing.channel_routing",
    },
}

0 个答案:

没有答案