django3.1 websocket,django3 websocket
本文主要介绍了框架中求转发到的具体使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
求转发到是一种持久化的协议,HTTP协议是一种无状态的协议,在特定场合我们需要使用长连接,做数据的实时更新,这种情况下我们就可以使用求转发到做持久连接。超文本传送协议(Hyper Text Transport Protocol的缩写)与求转发到二者存在交集。
后端:
从装饰者导入接受_websocket
导入数据
# 存储连接求转发到的用户
clist=[]
@accept_websocket
def websocketLink(请求):
# 获取连接
if request.is_websocket:
# 新增用户连接信息
clist.append(request.websocket)
# 监听接收客户端发送的消息或者客户端断开连接
对于request.websocket:中的消息
破裂
# 发送消息
def websocketMsg(客户端,消息):
b1=json.dumps(msg,确保_ascii=False).编码(“utf-8”)
client.send(b1)
# 服务端发送消息
def sendmsg():
sql=select * from customer
res=db1.find_all(sql)
if len(clist)0:
因为我在:列表中
I . send(JSON。转储({ list : RES },确保_ascii=False).编码( utf-8 ))
# websocketMsg(i,{list: res})
返回HttpResponse(“确定")
从日程安排者。调度程序。阻塞导入阻塞调度程序
def getecharts(请求):
scheduler=BlockingScheduler()
scheduler.add_job(sendmsg, interval ,seconds=1)
scheduler.start()
返回HttpResponse(“确定")
前端:
模板
div class=bgpic
van-row style= padding-top : 10px;填充-底部: 10px
van-col span=8
div id= week main style= width : 400 px;高度: 300像素/格
/范-科尔
van-col span= 8 http://API。地图。百度一下。com/marker/van-col
van-col span=8
div id= month main style= width : 400 px;高度: 300像素/格
/范-科尔
/van-row
范-罗
范-科尔span= 8 /范-科尔
范-科尔span= 8 /范-科尔
van-col span= 8"{信息列表1 } }/van-col
/van-row
/div
/模板
脚本
从“电子海图”导入*作为电子海图;
//从导入肌痛症./././https/我的axios ;
从" axios "导入axios
从“vue”导入{反应性};
导出默认值{
名称: 我一周,
setup(){
让信息列表
1 = 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);
//打印服务端返回的数据
infolist1.data = e.data
console.log(e.data)
// mydata = infolist1.list
// console.log(mydata)
};
socket.onclose=function(e){
console.log(e);
socket.close(); //关闭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>
到此这篇关于django中websocket的具体使用的文章就介绍到这了,更多相关django websocket内容请搜索盛行IT软件开发工作室以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT软件开发工作室!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。