MessageBridge.js 5.0 KB

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