提交 a38057f4 authored 作者: zhuyongshuai's avatar zhuyongshuai

优化websocket

上级 37d45d6d
/*
* @Author: Z 1518051043@qq.com
* @Date: 2025-12-01 13:52:20
* @LastEditors: zwy 1518051043@qq.com
* @LastEditTime: 2026-01-21 13:08:28
* @FilePath: \yuanxinPro\src\utils\requestSocekt.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import HeartbeatInterval from '@/store/module/app.js'
// const apiUrl = `ws://121.229.107.155:19021/trackInfo`
const apiUrl = `ws://121.229.107.155:19021/TestTimeSend`
const wss = new WebSocket(apiUrl);
// /*
// * @Author: Z 1518051043@qq.com
// * @Date: 2025-12-01 13:52:20
// * @LastEditors: zwy 1518051043@qq.com
// * @LastEditTime: 2026-01-21 13:08:28
// * @FilePath: \yuanxinPro\src\utils\requestSocekt.js
// * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
// */
// import useAppStore from '@/store/module/app.js'
// import { cs } from 'element-plus/es/locales.mjs';
// // const apiUrl = `ws://121.229.107.155:19021/trackInfo`
// const apiUrl = `ws://121.229.107.155:19021/TestTimeSend`
// const wss = new WebSocket(apiUrl);
// const appStore = useAppStore()
// wss.onopen = () => {
// console.log('WebSocket已连接11');
// };
// wss.onmessage = (event) => {
// console.log('接收到消息:', event.data);
// };
// wss.onerror = (error) => {
// console.error('WebSocket错误:', error);
// };
// wss.onclose = () => {
// clearInterval(HeartbeatInterval);
// //console.log('主星级WebSocket正在重新连接, attempting reconnect...');
// setTimeout(() => {
// // console.log('主星级WebSocket正在重新连接, attempting reconnect...');
// let timer = setTimeout(() => {
// // 自动重连逻辑
// wss = new WebSocket(apiUrl);
// //console.log('主星级WebSocket...',wss);
// wss = new WebSocket(apiUrl);
// console.log('主星级WebSocket...',wss);
// clearTimeout(timer);
// }, 1000);
// };
export default wss;
// // wss.onclose = () => {
// // clearInterval(HeartbeatInterval);
// // //console.log('主星级WebSocket正在重新连接, attempting reconnect...');
// // setTimeout(() => {
// // // 自动重连逻辑
// // wss = new WebSocket(apiUrl);
// // //console.log('主星级WebSocket...',wss);
// // }, 1000);
// // };
// export default wss;
import useAppStore from '@/store/module/app.js';
const apiUrl = `ws://121.229.107.155:19021/TestTimeSend`;
let wss = null;
let timer = null;
const appStore = useAppStore();
function createWebSocket() {
wss = new WebSocket(apiUrl);
wss.onopen = () => {
console.log('WebSocket已连接');
};
wss.onmessage = (event) => {
console.log('接收到消息:', event.data);
};
wss.onerror = (error) => {
console.error('WebSocket错误:', error);
};
wss.onclose = () => {
console.log('连接关闭,1秒后重连...');
timer = setTimeout(() => {
wss = createWebSocket(); // 重新创建连接
clearTimeout(timer);
timer = null; //释放内存
}, 1000);
};
}
// 初始化连接
if (!wss || wss.readyState === WebSocket.CLOSED) {
createWebSocket();
}
export default wss;
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论