import Stomp from "stompjs"; import store from '../src/store/index' var projectconfig = 'ws://192.168.10.14:8082/gyee-websocket' // ============================一般使用的变量============================ let number = 0; export const datainit = initialize; // ============================ 大函数体 ============================ function initialize(topic) { let adpClient = null; var url = projectconfig; // let socket = new SockJS(url); // 这个地址要找你们后端 // adpClient = Stomp.over(socket); adpClient = Stomp.client(url); adpClient.debug = null; adpClient.connect({topic:topic}, adpClient2 => connectCallBackSubscribe(adpClient), error => reconnect(error, adpClient)); } // 断线重连 function reconnect(error, adpClient) { //连接失败时再次调用函数 number++; adpClient.connected = false; clearTimeout(setTimeout(initialize('/topic/popup'), 1000 * 5)); debugX("DataAdapter reconnect:" + number + " 次"); return; } // ============================ 订阅函数体 ============================ function connectCallBackSubscribe(adpClient) { number = 0; adpClient.connected = true; adpClient.subscribe('/topic/popup', stompMessage => reflexWindturbineBasicInformation(stompMessage)); } // ============================ 解析函数体 ============================ function reflexWindturbineBasicInformation(stompMessage) { var newdata = JSON.parse(stompMessage.body); store.dispatch('getupdate', newdata); console.log(newdata) } // ============================ 其他 ============================ function debugX(text) { console.log(text); }