websocket.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import Stomp from "stompjs";
  2. import store from '../src/store/index'
  3. var projectconfig = 'ws://192.168.10.14:8082/gyee-websocket'
  4. // ============================一般使用的变量============================
  5. let number = 0;
  6. export const datainit = initialize;
  7. // ============================ 大函数体 ============================
  8. function initialize(topic) {
  9. let adpClient = null;
  10. var url = projectconfig;
  11. // let socket = new SockJS(url); // 这个地址要找你们后端
  12. // adpClient = Stomp.over(socket);
  13. adpClient = Stomp.client(url);
  14. adpClient.debug = null;
  15. adpClient.connect({topic:topic}, adpClient2 => connectCallBackSubscribe(adpClient), error => reconnect(error, adpClient));
  16. }
  17. // 断线重连
  18. function reconnect(error, adpClient) {
  19. //连接失败时再次调用函数
  20. number++;
  21. adpClient.connected = false;
  22. clearTimeout(setTimeout(initialize('/topic/popup'), 1000 * 5));
  23. debugX("DataAdapter reconnect:" + number + " 次");
  24. return;
  25. }
  26. // ============================ 订阅函数体 ============================
  27. function connectCallBackSubscribe(adpClient) {
  28. number = 0;
  29. adpClient.connected = true;
  30. adpClient.subscribe('/topic/popup', stompMessage => reflexWindturbineBasicInformation(stompMessage));
  31. }
  32. // ============================ 解析函数体 ============================
  33. function reflexWindturbineBasicInformation(stompMessage) {
  34. var newdata = JSON.parse(stompMessage.body);
  35. store.dispatch('getupdate', newdata);
  36. console.log(newdata)
  37. }
  38. // ============================ 其他 ============================
  39. function debugX(text) {
  40. console.log(text);
  41. }