MessageBridge.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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", "/topic/attention"]);
  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 === "/topic/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. if (this.observers.filter(item => item.key === msgs[i].key).length <= 5) {
  64. this.observers.push(msgs[i]);
  65. }
  66. }
  67. }
  68. /* 取消注册消息 */
  69. unregister(msgs) {
  70. let showIndex = null
  71. this.observers.forEach((item, index) => {
  72. if (item.key === msgs.key) {
  73. showIndex = index
  74. }
  75. })
  76. this.observers.splice(showIndex, 1);
  77. }
  78. reconnect() {
  79. setTimeout(() => {
  80. 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)) {
  81. store.commit('observers', false)
  82. }
  83. this.reconnect()
  84. }, 10000);
  85. if (((new Date()).getTime() - this.windFlag) > 20000) {
  86. console.log('心跳检测失败1,尝试重新连接');
  87. this.adapterSocket.disconnect()
  88. this.adapterSocket.connect()
  89. }
  90. if (((new Date()).getTime() - this.flag) > 20000) {
  91. console.log('心跳检测失败,尝试重新连接');
  92. this.calcSocket.disconnect()
  93. this.calcSocket.connect()
  94. }
  95. }
  96. }
  97. class WebSocket {
  98. onmessage;
  99. url;
  100. settings;
  101. client;
  102. constructor(url, onmessage, settings) {
  103. this.onerror = this.onerror.bind(this);
  104. this.connectCallBackSubscribe = this.connectCallBackSubscribe.bind(this);
  105. this.send = this.send.bind(this);
  106. this.connect = this.connect.bind(this);
  107. this.disconnect = this.disconnect.bind(this);
  108. this.onmessage = onmessage;
  109. this.url = url;
  110. this.settings = settings;
  111. // this.MessageBridge = new MessageBridge()
  112. try {
  113. this.connect();
  114. } catch (e) {
  115. // console.log("websocket连接错误:\n" + e);
  116. }
  117. }
  118. /* 连接 */
  119. connect() {
  120. this.client = Stomp.client(this.url);
  121. this.client.connect("", "", this.connectCallBackSubscribe, this.onerror);
  122. this.client.debug = null;
  123. }
  124. disconnect() {
  125. this.client.disconnect()
  126. }
  127. /* 检测连接是否正常 */
  128. protector() {
  129. }
  130. /* 注册 */
  131. connectCallBackSubscribe() {
  132. // console.log(`注册消息${this.settings}`)
  133. for (let index in this.settings) {
  134. this.client.subscribe(this.settings[index], frame => this.onmessage(frame));
  135. }
  136. }
  137. /* 发送 */
  138. send(destination, headers, body) {
  139. if (this.client.connected) {
  140. this.client.send(destination, headers, body);
  141. }
  142. }
  143. /* 发生错误 */
  144. onerror() {
  145. // console.log(`websocket [${this.url}] 连接出现错误:\n${error.message}`);
  146. // TODO 断线重连还有问题
  147. setTimeout(this.connect(), 5000);
  148. }
  149. }