Python中如何使用dwebsocket实现后端数据实时刷新-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

Python中如何使用dwebsocket实现后端数据实时刷新

本文小编为大家详细介绍“Python中如何使用dwebsocket实现后端数据实时刷新”,内容详细,步骤清晰,细节处理妥当,希望这篇“Python中如何使用dwebsocket实现后端数据实时刷新”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

创新互联是一家专注于网站制作、成都网站制作与策划设计,余江网站建设哪家好?创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:余江等地区。余江做网站价格咨询:18982081108

执行定时任务的时候,我们需要了解执行百分比或者实时数据返回,这时候可以采用的方法

1.ajax请求后端服务器,然后前端页面局部渲染获取百分比

2.使用webscoket进行长连接交流刷新

ajax使用方法使用interval函数来实现定时请求,本次这里不做说明

views.py文件添加如下内容

from django.shortcuts import render,HttpResponse
from dwebsocket.decorators import accept_websocket
import time,random
import uuid
import json
@accept_websocket
def test_websocket(request):
    cnt=1
    if request.is_websocket():
        while True:
            messages = {
                'time': time.strftime('%Y.%m.%d %H:%M:%S', time.localtime(time.time())),
                'server_msg': 'hello%s'%time.time(),
                'client_msg': 'msg%s'%time.time()
            }
            time.sleep(1)
            cnt+=1
            if cnt<=10:
                request.websocket.send(json.dumps(messages))
            else:
                break

def test_websocket_client(request):
    return  render(request,'websocket_client.html',locals())

settings.py文件增加dwebsocket

INSTALLED_APPS = [
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'dwebsocket'
]

urls.py文件添加相关链接

urlpatterns = [
    path('test_websocket', views.test_websocket, name='test_websocket'),
    path('test_websocket_client', views.test_websocket_client, name='test_websocket_client'),
]

直接上html代码




    
    dwebsocket实践
    
    
        $(function () {
            // $('#send_message').click(
            //     function() {
                var socket = new WebSocket("ws://" + window.location.host + "/test_websocket");
                socket.onopen = function () {
                    console.log('WebSocket open');//成功连接上Websocket
                    // socket.send($('#message').val());//发送数据到服务端
                };
                socket.onmessage = function (e) {
                    // console.log('message: ' + e.data);//打印服务端返回的数据
                    $('#messagecontainer').text('

' + JSON.parse(e.data).client_msg + '

'+'

' + JSON.parse(e.data).server_msg + '

');                     // $('#messagecontainer').text('

' + JSON.parse(e.data).server_msg + '

');                 };                 socket.onclose=function () {                     console.log("连接已关闭")                 }             // });         });               send message     

接受到消息

         

然后我们运行程序

Python中如何使用dwebsocket实现后端数据实时刷新

十秒之后断开连接得到了我们想要的结果

业务需求的话,可以在我们的test_websocket 修改我们的逻辑然后根据返回的结果进行渲染

读到这里,这篇“Python中如何使用dwebsocket实现后端数据实时刷新”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注创新互联行业资讯频道。


分享文章:Python中如何使用dwebsocket实现后端数据实时刷新
URL分享:http://kswsj.cn/article/jsodch.html

其他资讯