iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python中使用dwebsocket实现后端数据实时刷新
  • 106
分享到

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

Python dwebsocket数据刷新Python后端数据实时刷新 2023-05-18 05:05:49 106人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

执行定时任务的时候,我们需要了解执行百分比或者实时数据返回,这时候可以采用的方法 1.ajax请求后端服务器,然后前端页面局部渲染获取百分比 2.使用WEBscoket进行长连接交流

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

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代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>dwebsocket实践</title>
    <script  src="http://code.Jquery.com/jquery-1.11.1.min.js"></script>
    <script type="text/javascript">
        $(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('<p>' + JSON.parse(e.data).client_msg + '</p>'+'<p>' + JSON.parse(e.data).server_msg + '</p>');
                    // $('#messagecontainer').text('<p>' + JSON.parse(e.data).server_msg + '</p>');
                };
                socket.onclose=function () {
                    console.log("连接已关闭")
                }
            // });
        });
    </script>
</head>
<body>
    <input type="text" id="message" value="请输入发送消息!" />
    <button type="button" id="send_message">send message</button>
    <h1>接受到消息</h1>
    <div id="messagecontainer">
    </div>
</body>
</html>

然后我们运行程序

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

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

到此这篇关于python中使用dwebsocket实现后端数据实时刷新的文章就介绍到这了,更多相关Python dwebsocket内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Python中使用dwebsocket实现后端数据实时刷新

本文链接: https://www.lsjlt.com/news/211769.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作