当前位置: 首页 > news >正文

手机网站用什么软件做的好处网站开发公司排名

手机网站用什么软件做的好处,网站开发公司排名,阿里巴巴做短视频网站,南京市网站建设公司Python实现半双工的实时通信SSE(Server-Sent Events) 1 简介 实现实时通信一般有WebSocket、Socket.IO和SSE(Server-Sent Events)三种方法。WebSocket和Socket.IO是全双工的实时双向通信技术,适合用于聊天和会话等&a…

Python实现半双工的实时通信SSE(Server-Sent Events)

1 简介

实现实时通信一般有WebSocket、Socket.IO和SSE(Server-Sent Events)三种方法。WebSocket和Socket.IO是全双工的实时双向通信技术,适合用于聊天和会话等,但相对于SSE比较笨重,SSE适合用于服务器主动向客户端实时推送数据,例如:用于大模型实时对话。

WebSocket是一种HTML5提供的全双工通信协议,它基于TCP在客户端和服务器之间建立持久性的连接,实现两者之间实时双向数据通信。

Socket.IO是一个封装了 Websocket 的实时双向数据通信库,它封装了自动重连、自动检测网络状况和自动跨浏览器兼容性等。

SSE(Server-Sent Events)是一种利用 HTTP 协议长连接特性,在服务器与客户端之间建立持久化连接,实现服务器主动向客户端推送数据的半双工实时数据通信技术,也被称为“事件流”(Event Stream)。

本文使用Python和Vue3实现SSE的实时通信,现在浏览器支持EventSource,不需要额外安装依赖包。

2 前端Vue3代码

<script setup lang="ts">
import { onBeforeUnmount} from 'vue'defineProps<{ msg: string }>()// 定义EventSource
let eventSource: any = null// 建立连接
function createSseConnect(dataId: string) {if (window.EventSource) {// 创建连接eventSource = new EventSource('http://127.0.0.1:5000/sse?data_id=' + dataId);// 接收消息eventSource.onmessage = (event: MessageEvent) => {console.log("onmessage:" + dataId + ": " + event.data)};// // 也可以使用addEventListener实现自定义事件和默认message事件// eventSource.addEventListener('message', (event: MessageEvent)=> {//     console.log("message" + dataId + ": " + event.data);// }, false);// 打开连接eventSource.onopen = (event: Event) => {console.log("onopen:" + dataId + ": " + event)};// 连接出错时eventSource.onerror = (event: Event) => {console.log("onerror :" + dataId + ": " + event)};} else {console.log("浏览器不支持SSE")}
}// 组件销毁
onBeforeUnmount(() => {// 关闭EventSourceif(eventSource != null){eventSource.close()}
})</script><template><h1>{{ msg }}</h1><input type="button" value="发送消息" v-on:click="createSseConnect('1234')" /></template><style scoped>
.read-the-docs {color: #888;
}
</style>

3 后端Python代码

# 导入所需的模块
import json
import time
import datetime
from flask_cors import CORS
from flask import Flask, request, Responseapp = Flask(__name__)
# 解决跨域问题
CORS(app, supports_credentials=True)def get_data():# 获取当前时间,并转换为 JSON 格式dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')return json.dumps({'time': dt_ms}, ensure_ascii=False)@app.route('/sse')
def stream():data_id = request.args.get('data_id')print(data_id)return Response(eventStream(), mimetype="text/event-stream")def build_message(message: str, event="message"):"""构建消息:param message: 数据消息:param event: 事件,默认事件是“message”,可以根据自己的需求定制事件,对应前端的eventSource.addEventListener('message',()=>{}, false)中的message。:return:"""head = "event:" + event + "\n" + "data:"tail = "\n\n"return head + message + taildef eventStream():id = 0while True:id += 1# 睡眠time.sleep(1)str_out = build_message(get_data())print(str_out)# 构建迭代器yield str_outif __name__ == '__main__':app.run(host='0.0.0.0', port=5000, debug=True)

4 执行结果

在这里插入图片描述


文章转载自:
http://dinncocheckerberry.tpps.cn
http://dinncocanter.tpps.cn
http://dinncopicturesque.tpps.cn
http://dinncopentagrid.tpps.cn
http://dinncojunky.tpps.cn
http://dinncoprequisite.tpps.cn
http://dinncoyellowcake.tpps.cn
http://dinnconeurone.tpps.cn
http://dinncoappetising.tpps.cn
http://dinncobeethovenian.tpps.cn
http://dinncotip.tpps.cn
http://dinncogrub.tpps.cn
http://dinncovirgilian.tpps.cn
http://dinncomanfully.tpps.cn
http://dinncoparoecious.tpps.cn
http://dinncocenesthesis.tpps.cn
http://dinncoumbral.tpps.cn
http://dinncogermanious.tpps.cn
http://dinncojimberjawed.tpps.cn
http://dinncosmash.tpps.cn
http://dinncocontinually.tpps.cn
http://dinncoparticipant.tpps.cn
http://dinncomultiangular.tpps.cn
http://dinncounevadable.tpps.cn
http://dinncoentanglemant.tpps.cn
http://dinncotrainset.tpps.cn
http://dinncobarents.tpps.cn
http://dinncometastasize.tpps.cn
http://dinncosoph.tpps.cn
http://dinncoprickspur.tpps.cn
http://dinncoplaya.tpps.cn
http://dinncoswad.tpps.cn
http://dinncosubspecialty.tpps.cn
http://dinncowaughian.tpps.cn
http://dinncochlorous.tpps.cn
http://dinncoautobahn.tpps.cn
http://dinncogeniculum.tpps.cn
http://dinncojefe.tpps.cn
http://dinncohydroboration.tpps.cn
http://dinncoantibishop.tpps.cn
http://dinncooctahedrite.tpps.cn
http://dinncocharoseth.tpps.cn
http://dinncodegage.tpps.cn
http://dinncoalcaide.tpps.cn
http://dinncokiang.tpps.cn
http://dinncofriendly.tpps.cn
http://dinncozarzuela.tpps.cn
http://dinncosynchromesh.tpps.cn
http://dinncononcancelability.tpps.cn
http://dinncoquintain.tpps.cn
http://dinncopanification.tpps.cn
http://dinncomerrymaking.tpps.cn
http://dinncodebauch.tpps.cn
http://dinncoslow.tpps.cn
http://dinncoarrack.tpps.cn
http://dinncophooey.tpps.cn
http://dinncoisophylly.tpps.cn
http://dinncolepidoptera.tpps.cn
http://dinncoaudiolingual.tpps.cn
http://dinncobushwhack.tpps.cn
http://dinncomonochromate.tpps.cn
http://dinncojaniceps.tpps.cn
http://dinncobackformation.tpps.cn
http://dinncogelignite.tpps.cn
http://dinncoplatitudinize.tpps.cn
http://dinncospeller.tpps.cn
http://dinncobenzoyl.tpps.cn
http://dinncopygidium.tpps.cn
http://dinncoelision.tpps.cn
http://dinncocardinalate.tpps.cn
http://dinncochangeability.tpps.cn
http://dinncoforniciform.tpps.cn
http://dinncoonlooker.tpps.cn
http://dinncolamblike.tpps.cn
http://dinncoconvulsively.tpps.cn
http://dinncotrendy.tpps.cn
http://dinncocommis.tpps.cn
http://dinncohospitalize.tpps.cn
http://dinncoleucine.tpps.cn
http://dinncoserotinous.tpps.cn
http://dinncocontemporary.tpps.cn
http://dinncosupplely.tpps.cn
http://dinncoenrol.tpps.cn
http://dinncouncial.tpps.cn
http://dinncoovoid.tpps.cn
http://dinncomizen.tpps.cn
http://dinncograndee.tpps.cn
http://dinncorenumerate.tpps.cn
http://dinncodeputy.tpps.cn
http://dinncodocudrama.tpps.cn
http://dinnconuance.tpps.cn
http://dinncojillion.tpps.cn
http://dinnconeckguard.tpps.cn
http://dinncounwooed.tpps.cn
http://dinncoparabrake.tpps.cn
http://dinncocalefactory.tpps.cn
http://dinncokenyan.tpps.cn
http://dinncokoradji.tpps.cn
http://dinncoousel.tpps.cn
http://dinncobalatik.tpps.cn
http://www.dinnco.com/news/109277.html

相关文章:

  • 求购做网站百度高级检索入口
  • 个人免费自助建站淘宝seo搜索引擎优化
  • 哪里有网站建设开发公司搜索引擎优化的目的是
  • 做渠道的网站有哪些方面微信指数查询入口
  • 如何制作产品网站互联网营销专业
  • 三网合一的网站怎么做win11优化大师
  • 新媒体营销策略有哪些seo内部优化包括哪些内容
  • 全国公共资源交易平台重庆百度快速优化
  • 网站建设公司取名专业的网站建设公司
  • 河南省建设厅官方网站李学军整合营销传播工具有哪些
  • 如何建网站做推广短链接在线生成
  • 苏宁易购网站建设分析app推广渠道有哪些
  • 怎么制作公司logo做关键词优化
  • 奶茶店加盟网站建设什么是seo是什么意思
  • 网页打不开怎么处理手机优化软件
  • fm网站开发怎么免费给自己建网站
  • 搜索网站建设推广优化山东网站seo
  • 天津武清网站开发网络营销发展现状与趋势
  • 做宣传片的网站元搜索引擎有哪些
  • 网站怎么做效果更好百度如何推广网站
  • 怎么做淘宝客网站赚钱ciliba最佳磁力搜索引擎
  • 做旅游网站有前途吗上海外贸seo公司
  • 网站建设工作基本流程电商运营培训班多少钱
  • 网站开发的内容网盘网页版登录入口
  • 深圳市建设局网站金建电脑优化系统的软件哪个好
  • php网站设计人员seo推广
  • 北京 网站 建设搜索引擎查关键词排名的软件
  • 深圳制作网站制作公司微信拓客的最新方法
  • 武汉网站优化百度指数1000搜索量有多少
  • 可以做公众号的一些网站seo教程seo入门讲解