如何设置Django频道?

时间:2018-09-10 06:22:45

标签: python django django-channels

我对WebSocket有问题。我在管理面板中创建一个对象,并且前端调用将JSON对象获取到服务器。创建对象后,我最终需要在前端获得实时更新。

consumers.py

@receiver(post_save, sender=Factory)
def send_update(sender, instance, **kwargs):
    Group("liveblog").send({
        "text": json.dumps({
            "id": instance.id,
            "titile": instance.title,
            "choice": instance.choice,
            "address": instance.address
        })
    })

 class MyConsumer(JsonWebsocketConsumer):

    strict_ordering = False

    def connection_groups(self, **kwargs):
        return ["liveblog"]

    def ws_connect(self, message):
        Group("liveblog").add(message.reply_channel)
        message.reply_channel.send({"accept": True})


    def receive(self, content, **kwargs):
        multiplexer = kwargs['multiplexer']
        response = {
            "id": self.id,
            "title": self.title,
            "choice":self.choice,
            "address":self.address
        }
        multiplexer.send({"response":"OK"}) 
        multiplexer.group_send("liveblog", response)  

    def ws_disconnect(self, message):
        Group("liveblog").discard(message.reply_channel)

routing.py

channel_routing = [
    route_class(MyConsumer)
]

.js

var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var ws_path = ws_scheme + '://' + window.location.host;

var socket = new WebSocket("ws://localhost:8000");

socket.onopen = function () {
    console.log("Connected to socket");
  };
socket.onclose = function () {
    console.log("Disconnected from socket");
  }

我在做什么错了?

在浏览器中的控制台

Connected to socket
28[Violation] Added non-passive event listener to a scroll-blocking 
<some> event. Consider marking event handler as 'passive' to make the 
page more responsive. See <URL>
main.js:112 storage
main.js:113 []
main.js:88 sump
main.js:89 []
main.js:65 factorys
main.js:66 (6) [{…}, {…}, {…}, {…}, {…}, {…}]
main.js:135 petrol
main.js:136 []
main.js:155 line
main.js:156 []
inception.js:1 [Violation] 'message' handler took 349ms
[Violation] Forced reflow while executing JavaScript took 337ms

终端机中的控制台

[2018/09/10 13:06:00] HTTP GET /l/ 200 [0.05, 127.0.0.1:63006]
[2018/09/10 13:06:00] HTTP GET /robots.txt 404 [0.05, 127.0.0.1:63007]
[2018/09/10 13:06:00] WebSocket DISCONNECT / [127.0.0.1:63658]
[2018/09/10 13:06:01] HTTP GET /static/js/main.js 304 [0.05, 127.0.0.1:63006]
[2018/09/10 13:06:02] WebSocket HANDSHAKING / [127.0.0.1:63014]
[2018/09/10 13:06:02] WebSocket CONNECT / [127.0.0.1:63014]
[2018/09/10 13:06:03] HTTP GET /getStorage/ 200 [0.08, 127.0.0.1:63023]
[2018/09/10 13:06:03] HTTP GET /getPetrol/ 200 [0.08, 127.0.0.1:63024]
[2018/09/10 13:06:03] HTTP GET /getSump/ 200 [0.14, 127.0.0.1:63007]
[2018/09/10 13:06:03] HTTP GET /getLine/ 200 [0.11, 127.0.0.1:63025]
[2018/09/10 13:06:03] HTTP GET /getFactory/ 200 [0.16, 127.0.0.1:63006]

1 个答案:

答案 0 :(得分:1)

我认为您的套接字没有收到该消息。将以下侦听器添加到您的js文件中。

socket.onmessage = function(event) {
  alert(event.data);
};