MessageBridge.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Stomp from 'stompjs'
  2. export default class MessageBridge {
  3. observers;// 观察者
  4. calcSocket;// 后台websocket
  5. adapterSocket;// 适配器websocket
  6. constructor() {
  7. this.register = this.register.bind(this);
  8. this.unregister = this.unregister.bind(this);
  9. this.onmessage = this.onmessage.bind(this);
  10. this.getActions = this.getActions(this);
  11. this.observers = new Array();
  12. this.calcSocket = new WebSocket("ws://192.168.10.18:8099/wisdom_service", this.onmessage, ["/topic/suggestion", "/topic/sync-command-result", "/topic/fault-count",
  13. "/topic/alarm-count", "/topic/fault-popup", "/topic/popup-remove", "/topic/heartbeat-data"]);
  14. this.adapterSocket = new WebSocket("ws://192.168.10.18:8011/wisdom", this.onmessage, ["/topic/windturbine", "/topic/pv"]);
  15. }
  16. /* 单例 */
  17. static getInstance() {
  18. if (!MessageBridge.instance) {
  19. MessageBridge.instance = new MessageBridge();
  20. }
  21. return MessageBridge.instance;
  22. }
  23. /* 获得消息 */
  24. onmessage(msg) {
  25. if(msg.headers.data-type && msg.headers.data-type=="heartbeat"){
  26. console.log("获得心跳包!");
  27. return;
  28. }
  29. if (msg.command != "MESSAGE" || !msg.headers.destination) return;
  30. var os = this.getActions(msg.headers.destination);
  31. for (var id in os) {
  32. os[id].action(msg.body);
  33. }
  34. }
  35. getActions(destination){
  36. var list = new Array();
  37. for(var i in this.observers){
  38. if(this.observers[i].key==destination){
  39. list.push(this.observers[i]);
  40. }
  41. }
  42. return list;
  43. }
  44. /* 注册消息 */
  45. register(msgs) {
  46. for(var i in msgs){
  47. this.observers.push(msgs[i]);
  48. }
  49. }
  50. /* 取消注册消息 */
  51. unregister(msgs) {
  52. this.observers.remove(msgs);
  53. }
  54. }
  55. class WebSocket {
  56. onmessage;
  57. url;
  58. settings;
  59. client;
  60. constructor(url, onmessage, settings) {
  61. this.onerror = this.onerror.bind(this);
  62. this.connectCallBackSubscribe = this.connectCallBackSubscribe.bind(this);
  63. this.send = this.send.bind(this);
  64. this.onmessage = onmessage;
  65. this.url = url;
  66. this.settings = settings;
  67. try {
  68. this.connect();
  69. } catch (e) {
  70. console.log("websocket连接错误:\n" + e);
  71. }
  72. console.log("websocket");
  73. }
  74. /* 连接 */
  75. connect() {
  76. if (this.client != null) {
  77. this.client.close();
  78. }
  79. console.log(`正在连接websocket [${this.url}]`)
  80. this.client = Stomp.client(this.url);
  81. this.client.connect("", "", this.connectCallBackSubscribe, this.onerror);
  82. }
  83. /* 检测连接是否正常 */
  84. protector() {
  85. }
  86. /* 注册 */
  87. connectCallBackSubscribe() {
  88. for (var index in this.settings) {
  89. this.client.subscribe(this.settings[index], frame => this.onmessage(frame));
  90. }
  91. }
  92. /* 发送 */
  93. send(destination, headers, body) {
  94. if (this.client.connected) {
  95. this.client.send(destination, headers, body);
  96. }
  97. }
  98. /* 发生错误 */
  99. onerror(error) {
  100. console.log(`websocket [${this.url}] 连接出现错误:\n${error.message}`);
  101. // TODO 断线重连还有问题
  102. setTimeout(this.connect(), 5000);
  103. }
  104. }