|
|
@@ -5,19 +5,11 @@ let websocket: WebSocket | null = null; // 用于存储实例化后websocket
|
|
|
let rec: any; // 断线重连后,延迟5秒重新创建WebSocket连接 rec用来存储延迟请求的代码
|
|
|
// @ts-ignore
|
|
|
const baseUrl = import.meta.env.VITE_APP_WS_API;
|
|
|
-const store = useWSStore();
|
|
|
+
|
|
|
// 创建websocket
|
|
|
function creatWebSocket(wsUrl: string) {
|
|
|
- wsUrl = baseUrl+wsUrl;
|
|
|
- wsUrl = wsUrl.replace("http","ws");
|
|
|
- wsUrl = wsUrl.replace("https","wss");
|
|
|
- if(wsUrl.indexOf("ws") == -1){
|
|
|
- var start = window.location.href.split("://")[0];
|
|
|
- start = start.replace("http","ws");
|
|
|
- start = start.replace("https","wss");
|
|
|
- wsUrl =start+"://"+ window.location.href.split("://")[1].split("/")[0]+wsUrl
|
|
|
- }
|
|
|
- console.log("websocket==================");
|
|
|
+
|
|
|
+ console.log("websocket=================="+wsUrl);
|
|
|
// 判断当前浏览器是否支持WebSocket
|
|
|
if ("WebSocket" in window) {
|
|
|
console.log("当前浏览器支持 WebSocket");
|
|
|
@@ -31,19 +23,27 @@ function creatWebSocket(wsUrl: string) {
|
|
|
try {
|
|
|
initWebSocket(wsUrl); // 初始化websocket连接
|
|
|
} catch (e) {
|
|
|
+ debugger
|
|
|
console.log("尝试创建连接失败");
|
|
|
reConnect(wsUrl); // 如果无法连接上 webSocket 那么重新连接!可能会因为服务器重新部署,或者短暂断网等导致无法创建连接
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
// 初始化websocket
|
|
|
function initWebSocket(wsUrl: string) {
|
|
|
- websocket = new WebSocket(wsUrl);
|
|
|
- console.log("websocket:", websocket);
|
|
|
-
|
|
|
+ wsUrl = baseUrl+wsUrl;
|
|
|
+ wsUrl = wsUrl.replace("http","ws");
|
|
|
+ wsUrl = wsUrl.replace("https","wss");
|
|
|
|
|
|
+ if(wsUrl.indexOf("s://") == -1){
|
|
|
+ var start = window.location.href.split("://")[0];
|
|
|
+ start = start.replace("http","ws");
|
|
|
+ start = start.replace("https","wss");
|
|
|
+ wsUrl =start+"://"+ window.location.href.split("://")[1].split("/")[0]+wsUrl
|
|
|
+ }
|
|
|
+ websocket = new WebSocket(wsUrl);
|
|
|
+ // console.log("websocket:", websocket);
|
|
|
websocket.onopen = function () {
|
|
|
websocketOpen();
|
|
|
};
|
|
|
@@ -72,9 +72,9 @@ function initWebSocket(wsUrl: string) {
|
|
|
|
|
|
// 定义重连函数
|
|
|
let reConnect = (wsUrl: string) => {
|
|
|
-
|
|
|
+debugger
|
|
|
console.log("尝试重新连接");
|
|
|
- if (store.state.isConnected) return; // 如果已经连上就不在重连了
|
|
|
+ if (useWSStore().isConnected()) return; // 如果已经连上就不在重连了
|
|
|
rec && clearTimeout(rec);
|
|
|
rec = setTimeout(function () {
|
|
|
// 延迟5秒重连 避免过多次过频繁请求重连
|
|
|
@@ -86,57 +86,14 @@ let reConnect = (wsUrl: string) => {
|
|
|
// 创建连接
|
|
|
function websocketOpen() {
|
|
|
console.log("连接成功");
|
|
|
- store.connect(); // 修改连接状态
|
|
|
+ useWSStore().connect(); // 修改连接状态
|
|
|
}
|
|
|
// 数据接收
|
|
|
function websocketonmessage(e: MessageEvent<any>) {
|
|
|
- console.log("数据接收", e.data);
|
|
|
+ // console.log("数据接收", e.data);
|
|
|
const data = JSON.parse(e.data); // 解析JSON格式的数据
|
|
|
- store.state.message = data;
|
|
|
- // 下面的判断则是后台返回的接收到的数据 如何处理自己决定
|
|
|
- if (data.code === 400) {
|
|
|
- console.log("数据接收", data.msg);
|
|
|
- ElMessage({
|
|
|
- showClose: true,
|
|
|
- message: data.msg,
|
|
|
- type: 'warning',
|
|
|
- })
|
|
|
-
|
|
|
-
|
|
|
- } else if (data.code === 404) {
|
|
|
- ElMessage({
|
|
|
- showClose: true,
|
|
|
- message: data.msg,
|
|
|
- type: 'warning',
|
|
|
- })
|
|
|
- } else if (data.code === 0) {
|
|
|
- ElMessage({
|
|
|
- showClose: true,
|
|
|
- message: "连接成功",
|
|
|
-
|
|
|
+ useWSStore().setMessage(data); // 存储数据
|
|
|
|
|
|
- type: 'success',
|
|
|
- })
|
|
|
- } else if (data.code === 200) {
|
|
|
- ElMessage({
|
|
|
- showClose: true,
|
|
|
- message: data.msg,
|
|
|
- type: 'success',
|
|
|
- })
|
|
|
- // 成功后的相应处理 此处成功后播放音乐
|
|
|
- const audio = new Audio('./tipMusic.mp3');
|
|
|
- audio.play();
|
|
|
- } else {
|
|
|
- ElMessage({
|
|
|
- showClose: true,
|
|
|
- message: data.msg,
|
|
|
- type: 'error',
|
|
|
- })
|
|
|
- // 延时5秒后刷新页面
|
|
|
- setTimeout(() => {
|
|
|
- location.reload();
|
|
|
- }, 1000);
|
|
|
- }
|
|
|
|
|
|
// let data = JSON.parse(decodeUnicode(e.data))
|
|
|
}
|
|
|
@@ -146,7 +103,7 @@ function websocketonmessage(e: MessageEvent<any>) {
|
|
|
// 关闭
|
|
|
function websocketclose(e: any) {
|
|
|
console.log(e);
|
|
|
- store.disconnect(); // 修改连接状态
|
|
|
+ useWSStore().disconnect(); // 修改连接状态
|
|
|
console.log("connection closed (" + e.code + ")");
|
|
|
}
|
|
|
|
|
|
@@ -155,7 +112,7 @@ function websocketclose(e: any) {
|
|
|
// 数据发送
|
|
|
function websocketsend(data: any) {
|
|
|
console.log("发送的数据", data, JSON.stringify(data));
|
|
|
- if (websocket && store.state.isConnected) { // 检查连接状态
|
|
|
+ if (websocket && useWSStore().isConnected()) { // 检查连接状态
|
|
|
websocket.send(JSON.stringify(data));
|
|
|
|
|
|
} else {
|
|
|
@@ -173,7 +130,7 @@ function websocketsend(data: any) {
|
|
|
// 发送
|
|
|
function sendWebSocket(data: any) {
|
|
|
// 如果未保持连接状态 不允许直接发送消息 提示请选择连接设备
|
|
|
- if (!store.state.isConnected) {
|
|
|
+ if (!useWSStore().isConnected()) {
|
|
|
ElMessage({
|
|
|
showClose: true,
|
|
|
message: "请选择设备连接",
|
|
|
@@ -202,9 +159,16 @@ let closeWebSocket = () => {
|
|
|
};
|
|
|
|
|
|
|
|
|
+let dostartWebSocket = () => {
|
|
|
+ if (!websocket) {
|
|
|
+ initWebSocket("/ws/realtimedata/ss");
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
export {
|
|
|
initWebSocket,
|
|
|
sendWebSocket,
|
|
|
creatWebSocket,
|
|
|
closeWebSocket,
|
|
|
+ dostartWebSocket,
|
|
|
}
|