登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
我知道了
查看详情
登录
注册
开源中国、Gitee、模力方舟招人啦~ 快来看看有没有适合你的岗位
代码拉取完成,页面将自动刷新
开源项目
>
WEB应用开发
>
后台管理框架
&&
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
120
Star
1.8K
Fork
2.8K
疯狂的狮子Li
/
plus-ui
代码
Pull Requests
1
Wiki
统计
流水线
服务
Gitee Pages
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
99
任意页面刷新可重连websocket
已关闭
疯狂的狮子Li:dev
疯狂的狮子Li:ts
ploat
创建于 2024-04-17 17:08
克隆/下载
HTTPS
SSH
复制
下载 Email Patch
下载 Diff 文件
任意页面刷新可重连websocket 修改websocket.ts /** * @module initWebSocket 初始化 * @module websocketonopen 连接成功 * @module websocketonerror 连接失败 * @module websocketclose 断开连接 * @module resetHeart 重置心跳 * @module sendSocketHeart 心跳发送 * @module reconnect 重连 * @module sendMsg 发送数据 * @module websocketonmessage 接收数据 * @module test 测试收到消息传递 * @description socket 通信 * @param {any} url socket地址 * @param {any} websocket websocket 实例 * @param {any} heartTime 心跳定时器实例 * @param {number} socketHeart 心跳次数 * @param {number} HeartTimeOut 心跳超时时间 * @param {number} socketError 错误次数 */ import { getToken } from '@/utils/auth'; import useNoticeStore from '@/store/modules/notice'; import { ElNotification } from 'element-plus'; let socketUrl: any = ''; // socket地址 let websocket: any = null; // websocket 实例 let heartTime: any = null; // 心跳定时器实例 let socketHeart = 0 as number; // 心跳次数 const HeartTimeOut = 2000; // 心跳超时时间 10000 = 10s let socketError = 0 as number; // 错误次数 // 初始化socket export const initWebSocket = (url: any, addNotice: any) => { if (import.meta.env.VITE_APP_WEBSOCKET === 'false') { return; } socketUrl = url; // 初始化 websocket websocket = new WebSocket(url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID); websocketonopen(addNotice); websocketonmessage(addNotice); websocketonerror(); websocketclose(); sendSocketHeart(addNotice); return websocket; }; // socket 连接成功 export const websocketonopen = (addNotice: any) => { websocket.onopen = function () { console.log('连接 websocket 成功'); resetHeart(addNotice); }; }; // socket 连接失败 export const websocketonerror = () => { websocket.onerror = function (e: any) { console.log('连接 websocket 失败', e); }; }; // socket 断开链接 export const websocketclose = () => { websocket.onclose = function (e: any) { console.log('断开连接', e); }; }; // socket 重置心跳 export const resetHeart = (addNotice: any) => { socketHeart = 0; socketError = 0; clearInterval(heartTime); sendSocketHeart(addNotice); }; // socket心跳发送 export const sendSocketHeart = (addNotice: any) => { heartTime = setInterval(() => { // 如果连接正常则发送心跳 if (websocket.readyState == 1) { // if (socketHeart <= 30) { websocket.send( JSON.stringify({ type: 'ping' }) ); socketHeart = socketHeart + 1; } else { // 重连 reconnect(addNotice); } }, HeartTimeOut); }; // socket重连 export const reconnect = (addNotice: any) => { if (socketError <= 2) { clearInterval(heartTime); initWebSocket(socketUrl, addNotice); socketError = socketError + 1; // eslint-disable-next-line prettier/prettier console.log('socket重连', socketError); } else { // eslint-disable-next-line prettier/prettier console.log('重试次数已用完'); clearInterval(heartTime); } }; // socket 发送数据 export const sendMsg = (data: any) => { websocket.send(data); }; // socket 接收数据 export const websocketonmessage = (addNotice: any) => { websocket.onmessage = function (e: any) { if (e.data.indexOf('heartbeat') > 0) { resetHeart(addNotice); } if (e.data.indexOf('ping') > 0) { return; } console.log(JSON.parse(e.data)); addNotice({ message: JSON.parse(e.data).msg, skipUrl: JSON.parse(e.data).skipUrl, read: false, time: new Date().toLocaleString() }); ElNotification({ title: '消息', message: JSON.parse(e.data).msg, type: 'success', duration: 2000 }); return e.data; }; }; 修改Navbar.vue const { addNotice } = useNoticeStore(); onMounted(() => { nextTick(() => { // 这里写入你想在 DOM 更新完成后执行的代码 let protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://' initWebSocket(protocol + window.location.host + import.meta.env.VITE_APP_BASE_API + "/resource/websocket",addNotice); }); });
此 Pull Request 需要通过一些审核项
类型
指派人员
状态
审查
疯狂的狮子Li
进行中
(0/1人)
此 Pull Request 无法自动合并
尝试通过 WebIDE 解决冲突
怎样手动合并此 Pull Request
git checkout ts
git pull https://gitee.com/JavaLionLi/plus-ui.git dev
git push origin ts
评论
2
提交
150
文件
200+
检查
代码问题
0
批量操作
展开设置
折叠设置
审查
Code Owner
审查人员
May
sgs98
KonBAI
KonBAI-Q
三个三
thiszhc
玲娜贝er
dapppp
秋辞未寒
ColorDreams
孤舟烟雨
gzyy
疯狂的狮子Li
JavaLionLi
LiuHao
lhailgl
未设置
最少人数
1
测试
May
sgs98
KonBAI
KonBAI-Q
三个三
thiszhc
玲娜贝er
dapppp
秋辞未寒
ColorDreams
孤舟烟雨
gzyy
疯狂的狮子Li
JavaLionLi
LiuHao
lhailgl
未设置
最少人数
0
优先级
不指定
严重
主要
次要
不重要
标签
标签管理
未设置
关联 Issue
未关联
Pull Request 合并后将关闭上述关联 Issue
里程碑
未关联里程碑
参与者
(2)
Cherry-pick 提交
Cherry-pick 可以将
特定提交(Commit)
从某个分支挑选并应用到另一个分支,实现快速集成特定更改,而无需合并整个分支。
请选择应用 Cherry-pick 提交 (Commit) 的目标分支
新建分支
当前账号不存在 Fork 仓库,建议 cherry-pick 到 Fork 仓库。
Fork 仓库
提交列表
Commit SHA
Commit Message
基于 Cherry-pick 后的分支发起 Pull Request
取消
Cherry-pick
TypeScript
1
https://gitee.com/JavaLionLi/plus-ui.git
git@gitee.com:JavaLionLi/plus-ui.git
JavaLionLi
plus-ui
plus-ui
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册