我正在订购系统并使用django-channels向管理员发送有关已下订单的通知,并且在没有通知后它只能完成1-2个订单。
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)
def view(request):
# some order realted processing
message = {
"status": "Success",
"order-id": order.id
}
Group('admin-channel').send({
"text": json.dumps(message)
})
$(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);
};
});
CHANNEL_LAYERS = {
"default": {
"BACKEND": "asgi_redis.RedisChannelLayer",
"CONFIG": {
"hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
},
"ROUTING": "cashier.routing.channel_routing",
},
}