我试图使用频道但不工作 这是consumer.py
def ws_connect(message, cat_id):
try:
cat = Categories.objects.get(pk=cat_id)
except Categories.DoesNotExist:
pass
Group('cat-1').add(message.reply_channel)
def ws_diconnect(message, cat_id):
try:
cat = Categories.objects.get(pk=cat_id)
except Categories.DoesNotExist:
pass
Group('cat-1').discard(message.reply_channel)
这是routing.py:
channel_routing = [
route('websocket.receive', ws_connect, path=r'^/liveupdate/(?P<cat_id>\d+)/'),
route("websocket.disconnect", ws_diconnect, path=r'^/liveupdate/(?P<cat_id>\d+)/'),
]
这是signals.py:
@receiver(post_save, sender=Tender)
def send_update(sender, instance, created, raw, using, **kwargs):
print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>', instance, '2', raw, '3', using, '4', kwargs
data = json.dumps(
{'ministry': 'hisham',})
Group('cat-1').send({'tender': data,})
print 'Done'
这里是javascript:
<script type="application/javascript">
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
alert(ws_scheme +"://" + window.location.host + "/liveupdate/1/");
var socket = new WebSocket(ws_scheme +"://" + window.location.host + "/liveupdate/1/");
socket.onmessage = function(e) {
alert(e.data);
};
socket.onopen = function() {
console.log("Connected to socket");
};
socket.onclose = function() { console.log("Disconnected to socket"); }
</script>
当我试图保存招标信号时,浏览器中没有任何内容 即没有数据警报 我的代码有什么问题 任何想法
答案 0 :(得分:2)
使用 websocket.connect 代替 websocket.receive 。客户端将数据发送到服务器时使用接收。
route('websocket.connect', ws_connect, path=r'^/liveupdate/(?P<cat_id>\d+)/'),
您何时发送数据而非投标使用文字作为密钥。
Group('cat-1').send({'text': data,})