通过Django Channels& amp;将实时更新推送到客户端。的WebSockets [英] Push live updates to client through Django Channels & Websockets

查看:130
本文介绍了通过Django Channels& amp;将实时更新推送到客户端。的WebSockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个向客户端显示实时更新数据的页面。该网站的其余部分是使用Django构建的,所以我正在尝试使用Channels。

I'm trying to make a page which shows live-updating data to the client. The rest of the site is built with Django, so I'm trying to use Channels for this.

我显示的数据保存在JSON文件和MySQL数据库中,以便在网站的其他部分进行进一步计算。理想情况下,我希望在收到客户时显示收到的最新数据(即文件更新时)。

The data I am displaying is saved in both a JSON file and a MySQL database for further calculations in other parts of the site. Ideally, I would like to display the latest data received (that is, when the file updates) to the client as it is received.

即使我理解Channels是为此目的而构建的,但我很难做到这一点。

And even though as I understand Channels are built exactly for this purpose, I am having trouble doing it.

我已尝试从客户端发送多个请求,并在消费者中使用延迟和循环,但它(讽刺的是)只更新刷新或立即更新。但是,这些方法都不是由文件或数据库中的更改触发的。

I have tried sending multiple requests from the client-side with delay and loops in the consumer, but it either (ironically) only updates on refresh or updates instantly. However, neither of these approaches are triggered by a change in the file or database.

这是有效的代码,但并不真正做到需要的代码。 (不可否认,基本上没有任何东西......)

This is the code that "works", but doesn't really do what's required. (also, admittedly, there's basically nothing there...)

# consumers.py
def ws_connect(message):
  message.reply_channel.send({"accept": True})

def ws_receive(message):

  with open("data.json") as jsonfile:
    jsondata = json.load(jsonfile)

  res = json.dumps(jsondata)
  message.reply_channel.send({ "text": res, })

#routing.py
from channels.routing import route
from .consumers import ws_receive, ws_connect


channel_routing = [
  route("websocket.receive", ws_receive, path=r"^/websockets/$"),
  route("websocket.connect", ws_connect, path=r"^/websockets/$"),
]

JS使用:

<script>
var wsurl = "ws://" + "mywebsite.com" + "/websockets/";
socket = new WebSocket(wsurl);

socket.onopen = function() {
     socket.send("this is a request");
     console.log('sent');
  }

socket.onmessage = function(message) {
    console.log(message.data);
    document.getElementById("livedata").innerHTML = message.data;
}
</script>

我非常满意这些文档的链接可以帮助我实现这样的目标,因为我已经设法找不到整整一周的解决方案。

I'd be perfectly happy with a link to the docs that would help me achieve something like this, as I've managed to not find the solution for a whole week.

推荐答案

将用户添加到django渠道ws_connect上的小组

Add user to django channels Group on ws_connect

from channels.auth import channel_session_user_from_http
from channels import Group

@channel_session_user_from_http
def ws_connect(message, **kwargs):
    http_user = message.user
    if not http_user.is_anonymous:
        message.reply_channel.send({'accept': True})
        Group('user-'+str(http_user.id)).add(message.reply_channel)`

通过

Group('user-1').send({'text': 'hello user 1'})
Group('user-2').send({'text': 'hello user 2'})

这篇关于通过Django Channels&amp; amp;将实时更新推送到客户端。的WebSockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆