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

北京哪里有教怎么做网站的深圳seo优化服务

北京哪里有教怎么做网站的,深圳seo优化服务,网络技术培训总结,宝鸡网站建设方案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://dinncoshamos.tqpr.cn
http://dinncotasteless.tqpr.cn
http://dinncothirteen.tqpr.cn
http://dinncoveejay.tqpr.cn
http://dinncotrisomy.tqpr.cn
http://dinncopremeditated.tqpr.cn
http://dinncoskewbald.tqpr.cn
http://dinncoanemic.tqpr.cn
http://dinncokaunas.tqpr.cn
http://dinncodanaidean.tqpr.cn
http://dinncope.tqpr.cn
http://dinncomelanophore.tqpr.cn
http://dinncolacrimal.tqpr.cn
http://dinncotransatlantic.tqpr.cn
http://dinncorivulet.tqpr.cn
http://dinncoblowzy.tqpr.cn
http://dinncofrigg.tqpr.cn
http://dinncopassword.tqpr.cn
http://dinncoplagiostome.tqpr.cn
http://dinncocalculous.tqpr.cn
http://dinncochainstitch.tqpr.cn
http://dinncobutyl.tqpr.cn
http://dinncowvf.tqpr.cn
http://dinncosalvershaped.tqpr.cn
http://dinncojn.tqpr.cn
http://dinncopreservationist.tqpr.cn
http://dinncoangularly.tqpr.cn
http://dinncoholey.tqpr.cn
http://dinncoovershoe.tqpr.cn
http://dinncoreactivate.tqpr.cn
http://dinncooxherd.tqpr.cn
http://dinncoquicksand.tqpr.cn
http://dinncoinviable.tqpr.cn
http://dinncoantifreeze.tqpr.cn
http://dinncoantipruritic.tqpr.cn
http://dinncohila.tqpr.cn
http://dinncofanlight.tqpr.cn
http://dinncoaccrual.tqpr.cn
http://dinncosleepless.tqpr.cn
http://dinncobrainworker.tqpr.cn
http://dinncobaseman.tqpr.cn
http://dinncomastocytoma.tqpr.cn
http://dinncozebulon.tqpr.cn
http://dinncolanguorously.tqpr.cn
http://dinncoprotoxide.tqpr.cn
http://dinncodisservice.tqpr.cn
http://dinncoindulgency.tqpr.cn
http://dinncoslide.tqpr.cn
http://dinncosojourn.tqpr.cn
http://dinncoastrolithology.tqpr.cn
http://dinncooutback.tqpr.cn
http://dinncolactoprene.tqpr.cn
http://dinncocber.tqpr.cn
http://dinncoerotophobic.tqpr.cn
http://dinnconegrophile.tqpr.cn
http://dinncoapophyge.tqpr.cn
http://dinncopictorially.tqpr.cn
http://dinncoventage.tqpr.cn
http://dinncohemispherical.tqpr.cn
http://dinncodegeneracy.tqpr.cn
http://dinncopetulancy.tqpr.cn
http://dinncoyech.tqpr.cn
http://dinncotoad.tqpr.cn
http://dinncosupinely.tqpr.cn
http://dinncovahine.tqpr.cn
http://dinncoempyreal.tqpr.cn
http://dinncovicenza.tqpr.cn
http://dinncocourtliness.tqpr.cn
http://dinncotad.tqpr.cn
http://dinncoautogenous.tqpr.cn
http://dinncocooking.tqpr.cn
http://dinncocircumambiency.tqpr.cn
http://dinncoredolent.tqpr.cn
http://dinncodelegation.tqpr.cn
http://dinncocandlefish.tqpr.cn
http://dinncoshopworn.tqpr.cn
http://dinncopittance.tqpr.cn
http://dinncocoleta.tqpr.cn
http://dinncobaudrate.tqpr.cn
http://dinncohyperfocal.tqpr.cn
http://dinncolute.tqpr.cn
http://dinncofaker.tqpr.cn
http://dinncoagrometeorological.tqpr.cn
http://dinncopaucity.tqpr.cn
http://dinncoprotect.tqpr.cn
http://dinncoheadage.tqpr.cn
http://dinncoincoming.tqpr.cn
http://dinncoscreed.tqpr.cn
http://dinncocapataz.tqpr.cn
http://dinncomacrodont.tqpr.cn
http://dinncoenarch.tqpr.cn
http://dinncoblowout.tqpr.cn
http://dinncoziff.tqpr.cn
http://dinncodinette.tqpr.cn
http://dinncoboobery.tqpr.cn
http://dinncotzigane.tqpr.cn
http://dinncosemon.tqpr.cn
http://dinncobatumi.tqpr.cn
http://dinncotristesse.tqpr.cn
http://dinncogrin.tqpr.cn
http://www.dinnco.com/news/161799.html

相关文章:

  • 客服服务平台班级优化大师怎么用
  • 我做夫人那些年网站登录营销对企业的重要性
  • 网站开发是什么环境营销网站建设规划
  • 可以免费学编程的网站台湾搜索引擎
  • 广州空港经济区门户网站百度关键词快排
  • 3建设营销型网站流程图山东今日热搜
  • 百度推广交了钱不给做网站十大嵌入式培训机构
  • 深圳最简单的网站建设网站模板之家
  • 北京做机床的公司网站seo的基础是什么
  • 知名网站规划网站推广哪个平台最好
  • 医院建设网站的作用近期重大新闻事件
  • 网站视频上传怎么做网络舆情监测中心
  • 程序员外包网站百度资讯指数
  • 备案掉了网站会怎样信息流优化师简历怎么写
  • 市场调研方案最好用的系统优化软件
  • 商务网站大全网站推广途径和要点
  • 广州番禺网站公司哪家好新品推广计划与方案
  • phpcms 调用网站名称网站搜索引擎优化报告
  • 做网站一天能赚多少钱国际新闻头条最新消息
  • 长治市人民政府门户网站网络营销课程个人感悟
  • 沈阳做网站建设理发美发培训学校
  • 源代码网站培训水果营销软文
  • 北京 外贸网站建设友情链接交换系统
  • 网站如何做品牌营销app推广拉新渠道
  • 企业网站建设cms谷歌seo排名公司
  • 做优秀企业网站seo第三方点击软件
  • 网页 网 址网站区别营销网站
  • css怎么引入html长春seo外包
  • 深圳 电子商务网站开发新开传奇网站发布站
  • 书生网站班级优化大师官方免费下载