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

WebSocket异步导出

WebSocket异步导出

    • 1、安装sockjs-client和stompjs
    • 2、连接后台
    • 3、vite.config.ts 配置反向代理
    • 4、导出并实时通信
    • 5、 封装WebSocket 文件
    • 注册登录(城通网盘)

1、安装sockjs-client和stompjs

import SockJS from 'sockjs-client/dist/sockjs.min.js'
import Stomp from 'stompjs'

2、连接后台

let base_url = '/api/****'
//创建STOMP连接
let socket = new SockJS(sockUrl)
// 获取STOMP子协议的客户端对象
this.stompClient = Stomp.over(socket);
// 心跳发送频率
this.stompClient.heartbeat.outgoing = 60000;
// 心跳接收频率
this.stompClient.heartbeat.incoming = 120000;
// 调用.connect方法连接Stomp服务端进行验证
this.stompClient.connect({}, (frame) => {console.log('Connected: ' + frame);// 订阅服务端提供的某个topic  如用户idthis.stompClient.subscribe('/topic/***/' + this.$route.params.id, (frame) => {console.log('Received message:', JSON.stringify(frame.body));})
});

3、vite.config.ts 配置反向代理

原因:SockJS 使用https和http 传输

  // 服务端渲染server: {port: env.VITE_PORT, // 端口号host: "0.0.0.0",open: env.VITE_OPEN === 'true',// 本地跨域代理. 目前注释的原因:暂时没有用途,server 端已经支持跨域proxy: {['/api']: {target: 'http://*****/api',ws: false,rewrite: (path) => path.replace(new RegExp(^/api‘), ''),},}}

4、导出并实时通信

import WebSocketService from '@/utils/websocket'
const exportLoading = ref(false)  //异步导出加载中
const exportData = ref(false)     //导出结果提示
// 异步导出
const handleAsynExport = async () => {exportLoading.value = truetry {await AsynExportApi.exportSales(queryParams.value)if (WebSocketService?.stompClient == null) {// 建立 Websocket连接,  /topic/***/${id}  是后端提供的const destination = `/topic/***/${id}`WebSocketService.connect(destination, handleMessage)}} finally {setTimeout(() => {if (!exportData.value) {ElNotification.error({title: '提示',message: '导出失败,请稍后重试'})}exportLoading.value = false}, 3000)}
//弹窗下载链接
const handleMessage = (data: any) => {exportData.value = trueexportLoading.value = falseElNotification.success({title: '提示',message: h('p', null, [h('span', null, data.fileName + '加载成功,请'),h('a',{style: 'color: teal;cursor:pointer;text-decoration: underline;',onClick: () => {location.replace(data.filePath)}},'下载')])})
}//页面切换后断开连接
useRouter().afterEach(() => {if (WebSocketService?.stompClient !== null) {//断开Websocket连接WebSocketService.disconnect()}
})

5、 封装WebSocket 文件

下载 :WebSocket 封装 访问密码: 3146

注册登录(城通网盘)

http://www.lryc.cn/news/526059.html

相关文章:

  • OS2.【Linux】基本命令入门(1)
  • 【二叉树】4. 判断一颗二叉树是否是平衡二叉树。5. 对称二叉树。6. 二叉树的构建及遍历 7. 二叉树的分层遍历 。
  • OS Copilot功能测评:智能助手的炫彩魔法
  • MFC结构体数据文件读写实例
  • 音频 PCM 格式 - raw data
  • 关于deepin上运行Qt开发的程序
  • css 如何将字体进行压扁,即水平缩放scaleX
  • C++AVL树(二)详解
  • RocketMQ 的 Topic 和消息队列MessageQueue信息,是怎么分布到Broker的?怎么负载均衡到Broker的?
  • 无人机核心项目开发系列:从设计到实现的完整解析
  • 浅谈Redis
  • Ceisum无人机巡检直播视频投射
  • 【组件库】使用Vue2+AntV X6+ElementUI 实现拖拽配置自定义vue节点
  • Vue.js组件开发-如何实现全选反选
  • 2025.1.20——四、[强网杯 2019]Upload1 文件上传|反序列化
  • php代码审计2 piwigo CMS in_array()函数漏洞
  • docker搭建redis集群(三主三从)
  • [Datawheel]利用Zigent框架编写智能体-1
  • 【计算机视觉】人脸识别
  • linux环境变量配置文件区别 /etc/profile和~/.bash_profile
  • mac 配置 python 环境变量
  • 终极的复杂,是简单
  • 软件开发中的密码学(国密算法)
  • 【豆包MarsCode 蛇年编程大作战】蛇形烟花
  • Jmeter使用Request URL请求接口
  • 使用Pytest Fixtures来提升TestCase的可读性、高效性
  • Arduino大师练成手册 -- 读取DHT11
  • 【Jave全栈】Java与JavaScript比较
  • 【高项】6.2 定义活动 ITTO
  • openlava/LSF 用户组管理脚本