django中websocket的具體使用
websocket是一種持久化的協(xié)議,HTTP協(xié)議是一種無(wú)狀態(tài)的協(xié)議,在特定場(chǎng)合我們需要使用長(zhǎng)連接,做數(shù)據(jù)的實(shí)時(shí)更新,這種情況下我們就可以使用websocket做持久連接。http與websocket二者存在交集。
后端:
from dwebsocket.decorators import accept_websocket import json # 存儲(chǔ)連接websocket的用戶(hù) clist = [] ? @accept_websocket def websocketLink(request): ? ? # 獲取連接 ? ? if request.is_websocket: ? ? ? ? # 新增 用戶(hù) ?連接信息 ? ? ? ? clist.append(request.websocket) ? ? ? ? # 監(jiān)聽(tīng)接收客戶(hù)端發(fā)送的消息 或者 客戶(hù)端斷開(kāi)連接 ? ? ? ? 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: '特性示例:漸變色 陰影 點(diǎn)擊縮放', ? ? ? ? ? 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>
到此這篇關(guān)于django中websocket的具體使用的文章就介紹到這了,更多相關(guān)django websocket內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Python開(kāi)發(fā)云主機(jī)類(lèi)型管理腳本分享
這篇文章主要為大家詳細(xì)介紹了如何基于Python開(kāi)發(fā)一個(gè)云主機(jī)類(lèi)型管理腳本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02Python內(nèi)置庫(kù)之webbrowser模塊用法詳解
webbrowser模塊是Python自帶的標(biāo)準(zhǔn)庫(kù),無(wú)需安裝,可以直接在Python中使用該模塊來(lái)打開(kāi)網(wǎng)頁(yè)、PDF文件等,本文給大家詳細(xì)介紹了Python webbrowser模塊用法,需要的朋友可以參考下2023-08-08python3下使用cv2.imwrite存儲(chǔ)帶有中文路徑圖片的方法
今天小編就為大家分享一篇python3下使用cv2.imwrite存儲(chǔ)帶有中文路徑圖片的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05在Linux命令行終端中使用python的簡(jiǎn)單方法(推薦)
下面小編就為大家?guī)?lái)一篇在Linux命令行終端中使用python的簡(jiǎn)單方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01Python利用函數(shù)式編程實(shí)現(xiàn)優(yōu)化代碼
函數(shù)式編程(Functional Programming)是一種編程范式,它將計(jì)算視為函數(shù)的求值,并且避免使用可變狀態(tài)和循環(huán),在Python中還可以利用它的簡(jiǎn)潔和高效來(lái)解決實(shí)際問(wèn)題,下面我們就來(lái)學(xué)習(xí)一下它的具體用法吧2023-11-11centos+nginx+uwsgi+Django實(shí)現(xiàn)IP+port訪(fǎng)問(wèn)服務(wù)器
這篇文章主要介紹了centos+nginx+uwsgi+Django實(shí)現(xiàn)IP+port訪(fǎng)問(wèn)服務(wù)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11異步任務(wù)隊(duì)列Celery在Django中的使用方法
對(duì)于網(wǎng)站來(lái)說(shuō),給用戶(hù)一個(gè)較好的體驗(yàn)是很重要的事情,其中最重要的指標(biāo)就是網(wǎng)站的瀏覽速度。因此服務(wù)端要從各個(gè)方面對(duì)網(wǎng)站性能進(jìn)行優(yōu)化,這篇文章主要介紹了異步任務(wù)隊(duì)列Celery在Django中的使用方法,感興趣的小伙伴們可以參考一下2018-06-06