網(wǎng)站首頁 編程語言 正文
websocket是一種持久化的協(xié)議,HTTP協(xié)議是一種無狀態(tài)的協(xié)議,在特定場合我們需要使用長連接,做數(shù)據(jù)的實時更新,這種情況下我們就可以使用websocket做持久連接。http與websocket二者存在交集。
后端:
from dwebsocket.decorators import accept_websocket import json # 存儲連接websocket的用戶 clist = [] ? @accept_websocket def websocketLink(request): ? ? # 獲取連接 ? ? if request.is_websocket: ? ? ? ? # 新增 用戶 ?連接信息 ? ? ? ? clist.append(request.websocket) ? ? ? ? # 監(jiān)聽接收客戶端發(fā)送的消息 或者 客戶端斷開連接 ? ? ? ? for message in request.websocket: ? ? ? ? ? ? break ? ?# 發(fā)送消息 def websocketMsg(client, msg): ? ? b1 = json.dumps(msg,ensure_ascii=False).encode('utf-8') ? ? client.send(b1) ? # 服務(wù)端發(fā)送消息 def sendmsg(): ? ? sql = "select * from customer" ? ? res = db1.find_all(sql) ? ? if len(clist)>0: ? ? ? ? for i in clist: ? ? ? ? ? ? i.send(json.dumps({'list': res},ensure_ascii=False).encode('utf-8')) ? ? ? ? ? ? ?# websocketMsg(i, {'list': res}) ? ? return HttpResponse("ok") ? from apscheduler.schedulers.blocking import BlockingScheduler ? def getecharts(request): ? ? scheduler = BlockingScheduler() ? ? scheduler.add_job(sendmsg,'interval',seconds=1) ? ? scheduler.start() ? ? return HttpResponse('ok')
前端:
<template> ? <div class="bgpic"> ? ? <van-row style="padding-top: 10px;padding-bottom: 10px"> ? ? ? <van-col span="8"> ? ? ? ? <div id="weekmain" style="width: 400px;height: 300px"></div> ? ? ? </van-col> ? ? ? <van-col span="8">http://api.map.baidu.com/marker </van-col> ? ? ? <van-col span="8"> ? ? ? ? <div id="monthmain" style="width: 400px;height: 300px"></div> ? ? ? </van-col> ? ? </van-row> ? ? <van-row> ? ? ? <van-col span="8"></van-col> ? ? ? <van-col span="8"></van-col> ? ? ? <van-col span="8">{{infolist1}}</van-col> ? ? </van-row> ? </div> </template> ? <script> import * as echarts from 'echarts'; // import myaxios from "../../../https/myaxios"; import axios from 'axios'; import {reactive} from 'vue'; export default { ? name: "myweek", ? setup(){ ? ? let infolist1 = reactive({"data":[]}) ? ? // let mydata = reactive([]) ? ? const initdata=()=>{ ? ? ? var socket = new WebSocket("ws:127.0.0.1:8000/websocketLink"); ? ? ? ? socket.onopen = function () { ? ? ? ? console.log('連接成功');//成功連接上Websocket ? ? ? }; ? ? ? socket.onmessage = function (e) { ? ? ? ? // alert('消息提醒: ' + e.data); ? ? ? ? //打印服務(wù)端返回的數(shù)據(jù) ? ? ? ? infolist1.data = e.data ? ? ? ? console.log(e.data) ? ? ? ? // mydata = infolist1.list ? ? ? ? // console.log(mydata) ? ? ? }; ? ? ? socket.onclose=function(e){ ? ? ? ? console.log(e); ? ? ? ? socket.close(); //關(guān)閉TCP連接 ? ? ? }; ? ? } ? ? return{ ? ? ? infolist1, ? ? ? initdata ? ? } ? }, ? data(){ ? ? return{ ? ? ? infolist:[], ? ? } ? }, ? methods:{ ? ? mget(){ ? ? ? axios.get("http://127.0.0.1:8000/getecharts").then(res=>{ ? ? ? ? console.log(res) ? ? ? }) ? ? }, ? ? infoshow(){ ? ? ? axios.get("http://localhost:8000/infoshow","get").then(res=>{ ? ? ? ? this.infolist=res.data.list ? ? ? ? this.getmonth() ? ? ? ? this.mget() ? ? ? }) ? ? }, ? ? getmonth(){ ? ? ? var chartDom = document.getElementById('monthmain'); ? ? ? var myChart = echarts.init(chartDom); ? ? ? var option; ? // prettier-ignore ? ? ? ? let dataAxis = []; // prettier-ignore ? ? ? let data = []; ? ? ? ? for(let i=0;i<this.infolist.length;i++){ ? ? ? ? dataAxis.push(this.infolist[i]["name"]) ? ? ? ? data.push(this.infolist[i]["tmoney"]) ? ? ? } ? ? ? ? let yMax = 10000; ? ? ? let dataShadow = []; ? ? ? for (let i = 0; i < data.length; i++) { ? ? ? ? dataShadow.push(yMax); ? ? ? } ? ? ? option = { ? ? ? ? title: { ? ? ? ? ? text: '特性示例:漸變色 陰影 點擊縮放', ? ? ? ? ? subtext: 'Feature Sample: Gradient Color, Shadow, Click Zoom' ? ? ? ? }, ? ? ? ? xAxis: { ? ? ? ? ? data: dataAxis, ? ? ? ? ? axisLabel: { ? ? ? ? ? ? inside: true, ? ? ? ? ? ? color: '#fff' ? ? ? ? ? }, ? ? ? ? ? axisTick: { ? ? ? ? ? ? show: false ? ? ? ? ? }, ? ? ? ? ? axisLine: { ? ? ? ? ? ? show: false ? ? ? ? ? }, ? ? ? ? ? z: 10 ? ? ? ? }, ? ? ? ? yAxis: { ? ? ? ? ? axisLine: { ? ? ? ? ? ? show: false ? ? ? ? ? }, ? ? ? ? ? axisTick: { ? ? ? ? ? ? show: false ? ? ? ? ? }, ? ? ? ? ? axisLabel: { ? ? ? ? ? ? color: '#999' ? ? ? ? ? } ? ? ? ? }, ? ? ? ? dataZoom: [ ? ? ? ? ? { ? ? ? ? ? ? type: 'inside' ? ? ? ? ? } ? ? ? ? ], ? ? ? ? series: [ ? ? ? ? ? { ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? showBackground: true, ? ? ? ? ? ? itemStyle: { ? ? ? ? ? ? ? color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ ? ? ? ? ? ? ? ? { offset: 0, color: '#83bff6' }, ? ? ? ? ? ? ? ? { offset: 0.5, color: '#188df0' }, ? ? ? ? ? ? ? ? { offset: 1, color: '#188df0' } ? ? ? ? ? ? ? ]) ? ? ? ? ? ? }, ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? itemStyle: { ? ? ? ? ? ? ? ? color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [ ? ? ? ? ? ? ? ? ? { offset: 0, color: '#2378f7' }, ? ? ? ? ? ? ? ? ? { offset: 0.7, color: '#2378f7' }, ? ? ? ? ? ? ? ? ? { offset: 1, color: '#83bff6' } ? ? ? ? ? ? ? ? ]) ? ? ? ? ? ? ? } ? ? ? ? ? ? }, ? ? ? ? ? ? data: data ? ? ? ? ? } ? ? ? ? ] ? ? ? }; // Enable data zoom when user click bar. ? ? ? const zoomSize = 6; ? ? ? myChart.on('click', function (params) { ? ? ? ? console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)]); ? ? ? ? myChart.dispatchAction({ ? ? ? ? ? type: 'dataZoom', ? ? ? ? ? startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)], ? ? ? ? ? endValue: ? ? ? ? ? ? ? dataAxis[Math.min(params.dataIndex + zoomSize / 2, data.length - 1)] ? ? ? ? }); ? ? ? }); ? ? ? ? option && myChart.setOption(option); ? ? }, ? ? getweek(){ ? ? ? var chartDom = document.getElementById('weekmain'); ? ? ? var myChart = echarts.init(chartDom); ? ? ? var option; ? ? ? ? option = { ? ? ? ? tooltip: { ? ? ? ? ? trigger: 'axis', ? ? ? ? ? axisPointer: { ? ? ? ? ? ? type: 'shadow' ? ? ? ? ? } ? ? ? ? }, ? ? ? ? legend: {}, ? ? ? ? grid: { ? ? ? ? ? left: '3%', ? ? ? ? ? right: '4%', ? ? ? ? ? bottom: '3%', ? ? ? ? ? containLabel: true ? ? ? ? }, ? ? ? ? xAxis: [ ? ? ? ? ? { ? ? ? ? ? ? type: 'category', ? ? ? ? ? ? data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] ? ? ? ? ? } ? ? ? ? ], ? ? ? ? yAxis: [ ? ? ? ? ? { ? ? ? ? ? ? type: 'value' ? ? ? ? ? } ? ? ? ? ], ? ? ? ? series: [ ? ? ? ? ? { ? ? ? ? ? ? name: 'Direct', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? data: [320, 332, 301, 334, 390, 330, 320] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: 'Email', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? stack: 'Ad', ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? data: [120, 132, 101, 134, 90, 230, 210] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: 'Union Ads', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? stack: 'Ad', ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? data: [220, 182, 191, 234, 290, 330, 310] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: 'Video Ads', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? stack: 'Ad', ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? data: [150, 232, 201, 154, 190, 330, 410] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: 'Search Engine', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? data: [862, 1018, 964, 1026, 1679, 1600, 1570], ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? markLine: { ? ? ? ? ? ? ? lineStyle: { ? ? ? ? ? ? ? ? type: 'dashed' ? ? ? ? ? ? ? }, ? ? ? ? ? ? ? data: [[{ type: 'min' }, { type: 'max' }]] ? ? ? ? ? ? } ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: 'Baidu', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? barWidth: 5, ? ? ? ? ? ? stack: 'Search Engine', ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? data: [620, 732, 701, 734, 1090, 1130, 1120] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: 'Google', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? stack: 'Search Engine', ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? data: [120, 132, 101, 134, 290, 230, 220] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: 'Bing', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? stack: 'Search Engine', ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? data: [60, 72, 71, 74, 190, 130, 110] ? ? ? ? ? }, ? ? ? ? ? { ? ? ? ? ? ? name: 'Others', ? ? ? ? ? ? type: 'bar', ? ? ? ? ? ? stack: 'Search Engine', ? ? ? ? ? ? emphasis: { ? ? ? ? ? ? ? focus: 'series' ? ? ? ? ? ? }, ? ? ? ? ? ? data: [62, 82, 91, 84, 109, 110, 120] ? ? ? ? ? } ? ? ? ? ] ? ? ? }; ? ? ? ? option && myChart.setOption(option); ? ? ? }, ? }, ? mounted() { ? ? this.infoshow() ? ? this.getweek() ? ? this.initdata() ? } } </script> ? <style scoped> .bgpic{ ? background-image: url("../../../https/4.jpg"); ? width: 1269px; ? height: 781px; } </style>
原文鏈接:https://blog.csdn.net/LAM1006_csdn/article/details/122310064
相關(guān)推薦
- 2022-12-13 Python按天實現(xiàn)生成時間范圍序列的方法詳解_python
- 2023-01-13 Go?cobra庫使用教程_Golang
- 2022-11-23 Android集成GreenDao數(shù)據(jù)庫的操作步驟_Android
- 2022-12-30 React錯誤邊界Error?Boundaries詳解_React
- 2022-11-07 .NET?實現(xiàn)啟動時重定向程序運行路徑及?Windows?服務(wù)運行模式部署的方法_實用技巧
- 2023-07-15 監(jiān)聽鼠標mouse事件冒泡處理
- 2022-12-07 C語言內(nèi)存分布與heap空間分別詳細講解_C 語言
- 2022-08-22 Golang實現(xiàn)快速求冪的方法詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支