websocket.js 1.4 KB

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