MessageBridge.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { setTimeout } from 'core-js';
  2. import Stomp from 'stompjs'
  3. import store from '../store/index'
  4. export default class MessageBridge {
  5. observers;// 观察者
  6. calcSocket;// 后台websocket
  7. adapterSocket;// 适配器websocket
  8. flag;
  9. windFlag;
  10. flagArr;
  11. constructor() {
  12. this.register = this.register.bind(this);
  13. this.unregister = this.unregister.bind(this);
  14. this.onmessage = this.onmessage.bind(this);
  15. this.getActions = this.getActions.bind(this);
  16. this.observers = new Array();
  17. this.flagArr = new Array();
  18. this.flag = (new Date()).getTime();
  19. this.windFlag = (new Date()).getTime();
  20. this.reconnect()
  21. this.calcSocket = new WebSocket(`ws://${process.env.VUE_APP_APIS}/wisdom_service`, this.onmessage, ["/topic/suggestion", "/topic/sync-command-result", "/topic/fault-count",
  22. "/topic/alarm-count", "/topic/fault-popup", "/topic/popup-remove", "/topic/heartbeat-data", "/topic/voice-control", "/topic/title-info"]);
  23. this.adapterSocket = new WebSocket(`ws://${process.env.VUE_APP_ADAPTERURLS}/wisdom`, this.onmessage, ["/topic/windturbine", "/topic/pv"]);
  24. }
  25. /* 单例 */
  26. static getInstance() {
  27. if (!MessageBridge.instance) {
  28. MessageBridge.instance = new MessageBridge();
  29. }
  30. return MessageBridge.instance;
  31. }
  32. /* 获得消息 */
  33. onmessage(msg) {
  34. if (msg.headers.destination === "title-info") {
  35. this.flag = (new Date()).getTime()
  36. }
  37. if (msg.headers.destination === "/topic/windturbine") {
  38. this.windFlag = (new Date()).getTime()
  39. }
  40. if (msg.command != "MESSAGE" || !msg.headers.destination) return;
  41. let os = this.getActions(msg.headers.destination);
  42. for (let id in os) {
  43. try {
  44. os[id].action(msg.body, msg.headers);
  45. } catch (e) { }
  46. }
  47. }
  48. getActions(destination) {
  49. let list = new Array();
  50. if ((this.observers.filter(item => item.key === "/topic/windturbine").length === 0) ||(this.observers.filter(item => item.key === "/topic/suggestion").length === 0) ||(this.observers.filter(item => item.key === "/topic/title-info").length === 0) ) {
  51. store.commit('observers', false)
  52. }
  53. for (let i in this.observers) {
  54. if (this.observers[i].key == destination) {
  55. list.push(this.observers[i]);
  56. }
  57. }
  58. return list;
  59. }
  60. /* 注册消息 */
  61. register(msgs) {
  62. for (let i in msgs) {
  63. this.observers.push(msgs[i]);
  64. }
  65. }
  66. /* 取消注册消息 */
  67. unregister(msgs) {
  68. let showIndex = null
  69. this.observers.forEach((item, index) => {
  70. if (item.key === msgs.key) {
  71. showIndex = index
  72. }
  73. })
  74. this.observers.splice(showIndex, 1);
  75. }
  76. reconnect() {
  77. setTimeout(() => {
  78. if ((this.observers.filter(item => item.key === "/topic/windturbine").length === 0) ||(this.observers.filter(item => item.key === "/topic/suggestion").length === 0) ||(this.observers.filter(item => item.key === "/topic/title-info").length === 0) ) {
  79. store.commit('observers', false)
  80. }
  81. this.reconnect()
  82. }, 10000);
  83. if (((new Date()).getTime() - this.windFlag) > 20000) {
  84. console.log('心跳检测失败1,尝试重新连接');
  85. this.adapterSocket.disconnect()
  86. this.adapterSocket.connect()
  87. }
  88. if (((new Date()).getTime() - this.flag) > 20000) {
  89. console.log('心跳检测失败,尝试重新连接');
  90. this.calcSocket.disconnect()
  91. this.calcSocket.connect()
  92. }
  93. }
  94. }
  95. class WebSocket {
  96. onmessage;
  97. url;
  98. settings;
  99. client;
  100. constructor(url, onmessage, settings) {
  101. this.onerror = this.onerror.bind(this);
  102. this.connectCallBackSubscribe = this.connectCallBackSubscribe.bind(this);
  103. this.send = this.send.bind(this);
  104. this.connect = this.connect.bind(this);
  105. this.disconnect = this.disconnect.bind(this);
  106. this.onmessage = onmessage;
  107. this.url = url;
  108. this.settings = settings;
  109. // this.MessageBridge = new MessageBridge()
  110. try {
  111. this.connect();
  112. } catch (e) {
  113. // console.log("websocket连接错误:\n" + e);
  114. }
  115. }
  116. /* 连接 */
  117. connect() {
  118. this.client = Stomp.client(this.url);
  119. this.client.connect("", "", this.connectCallBackSubscribe, this.onerror);
  120. this.client.debug = null;
  121. }
  122. disconnect() {
  123. this.client.disconnect()
  124. }
  125. /* 检测连接是否正常 */
  126. protector() {
  127. }
  128. /* 注册 */
  129. connectCallBackSubscribe() {
  130. // console.log(`注册消息${this.settings}`)
  131. for (let index in this.settings) {
  132. this.client.subscribe(this.settings[index], frame => this.onmessage(frame));
  133. }
  134. }
  135. /* 发送 */
  136. send(destination, headers, body) {
  137. if (this.client.connected) {
  138. this.client.send(destination, headers, body);
  139. }
  140. }
  141. /* 发生错误 */
  142. onerror() {
  143. // console.log(`websocket [${this.url}] 连接出现错误:\n${error.message}`);
  144. // TODO 断线重连还有问题
  145. setTimeout(this.connect(), 5000);
  146. }
  147. }