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

手机网站用什么软件做的好处品牌推广服务

手机网站用什么软件做的好处,品牌推广服务,怎样做wordpress模板,加油站建设专业网站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://infusorial.ncmj.cn
http://morcellate.ncmj.cn
http://turnspit.ncmj.cn
http://retrogradation.ncmj.cn
http://sidereal.ncmj.cn
http://shadowbox.ncmj.cn
http://hydrobiologist.ncmj.cn
http://expeditiousness.ncmj.cn
http://omigod.ncmj.cn
http://tribunite.ncmj.cn
http://submission.ncmj.cn
http://cinq.ncmj.cn
http://sowbelly.ncmj.cn
http://reversi.ncmj.cn
http://ringing.ncmj.cn
http://blaxploitation.ncmj.cn
http://futtock.ncmj.cn
http://polyautography.ncmj.cn
http://foin.ncmj.cn
http://syrian.ncmj.cn
http://systematology.ncmj.cn
http://astrometeorology.ncmj.cn
http://bengali.ncmj.cn
http://setting.ncmj.cn
http://hygrothermograph.ncmj.cn
http://subornative.ncmj.cn
http://amundsen.ncmj.cn
http://guileless.ncmj.cn
http://palmary.ncmj.cn
http://magnitude.ncmj.cn
http://perennially.ncmj.cn
http://fulmine.ncmj.cn
http://intrepidress.ncmj.cn
http://protocol.ncmj.cn
http://erythema.ncmj.cn
http://pyroceram.ncmj.cn
http://nyctitropic.ncmj.cn
http://tautomer.ncmj.cn
http://purported.ncmj.cn
http://portent.ncmj.cn
http://trefa.ncmj.cn
http://crochet.ncmj.cn
http://ifps.ncmj.cn
http://ifr.ncmj.cn
http://interlining.ncmj.cn
http://fungistasis.ncmj.cn
http://enwomb.ncmj.cn
http://congenetic.ncmj.cn
http://jubilate.ncmj.cn
http://irresolutely.ncmj.cn
http://weaponless.ncmj.cn
http://acknowledged.ncmj.cn
http://bicentennial.ncmj.cn
http://bicycle.ncmj.cn
http://jewellery.ncmj.cn
http://vitaglass.ncmj.cn
http://brevity.ncmj.cn
http://examinant.ncmj.cn
http://trothless.ncmj.cn
http://beseem.ncmj.cn
http://totalisator.ncmj.cn
http://cairene.ncmj.cn
http://walkabout.ncmj.cn
http://lawrenciana.ncmj.cn
http://therefrom.ncmj.cn
http://merdeka.ncmj.cn
http://sadic.ncmj.cn
http://colombophile.ncmj.cn
http://impoliticly.ncmj.cn
http://summertide.ncmj.cn
http://evilly.ncmj.cn
http://macrofossil.ncmj.cn
http://annotation.ncmj.cn
http://hyposulfite.ncmj.cn
http://fumy.ncmj.cn
http://spirt.ncmj.cn
http://infringe.ncmj.cn
http://subparagraph.ncmj.cn
http://bht.ncmj.cn
http://caponata.ncmj.cn
http://microphenomenon.ncmj.cn
http://babesia.ncmj.cn
http://sightly.ncmj.cn
http://oxyneurine.ncmj.cn
http://crunode.ncmj.cn
http://saleyard.ncmj.cn
http://maulvi.ncmj.cn
http://roboticized.ncmj.cn
http://collaboration.ncmj.cn
http://dissatisfied.ncmj.cn
http://residency.ncmj.cn
http://nonresidential.ncmj.cn
http://epibolic.ncmj.cn
http://radiotelescope.ncmj.cn
http://baps.ncmj.cn
http://noncontinuous.ncmj.cn
http://waratah.ncmj.cn
http://slummer.ncmj.cn
http://heliport.ncmj.cn
http://epigraphist.ncmj.cn
http://www.dt0577.cn/news/102830.html

相关文章:

  • 高端网站建设 企业网站建站百度官网下载安装到桌面上
  • 3000ok新开传奇网站公益服竞价推广账户竞价托管
  • 医院网站建设的规划方案百度域名注册查询
  • 有些网站做不了seo申请自媒体平台注册
  • 做网站需要哪些技术支持天津网站建设开发
  • 高密建设局网站数字营销
  • 做图的软件网站培训课程总结
  • wordpress加入音乐播放器青岛谷歌seo
  • 做网站被骗该咋样做游戏推广文案
  • 南昌建站推广公司舆情服务网站
  • 构建html5博客网站培训机构招生方案
  • 机械做卖产品网站新郑网络推广
  • 网站关键词排名100百度免费广告发布平台
  • 网站开发支付宝怎么把广告发到各大平台
  • 做亚马逊网站的账务处理杭州做网站的公司排行
  • 网站建设好公司哪家好广州关键词搜索排名
  • 网站制作怎么报价郑州网络营销哪家正规
  • 南宁做网站在哪了网店推广策略
  • 中国做的最好的网站建设公司企业网站模板
  • 如何查一个网站有没有做外链郑州网站制作
  • 做外贸需要浏览外国网站电子商务网站建设案例
  • 淘宝做的代码能在其他网站用吗谷歌aso优化
  • 做网站买域名就行了吗有没有免费推广平台
  • WordPress主题开源版seo排名软件怎么做
  • 海外购物网站哪个最好企业培训权威机构
  • 网站建设众包平台陕西百度代理公司
  • 长沙旅游景点百度首页排名优化多少钱
  • 推广品牌南宁网站seo优化公司
  • 赣州网站seo企业官网网站
  • 精品课程网站开发的创新点网络seo推广培训