MessageBridge.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.bind(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. try{
  33. os[id].action(msg.body);
  34. }catch(e){console.log(e);}
  35. }
  36. }
  37. getActions(destination){
  38. var list = new Array();
  39. for(var i in this.observers){
  40. if(this.observers[i].key==destination){
  41. list.push(this.observers[i]);
  42. }
  43. }
  44. return list;
  45. }
  46. /* 注册消息 */
  47. register(msgs) {
  48. for(var i in msgs){
  49. this.observers.push(msgs[i]);
  50. }
  51. }
  52. /* 取消注册消息 */
  53. unregister(msgs) {
  54. this.observers.remove(msgs);
  55. }
  56. }
  57. class WebSocket {
  58. onmessage;
  59. url;
  60. settings;
  61. client;
  62. constructor(url, onmessage, settings) {
  63. this.onerror = this.onerror.bind(this);
  64. this.connectCallBackSubscribe = this.connectCallBackSubscribe.bind(this);
  65. this.send = this.send.bind(this);
  66. this.connect=this.connect.bind(this);
  67. this.onmessage = onmessage;
  68. this.url = url;
  69. this.settings = settings;
  70. try {
  71. this.connect();
  72. } catch (e) {
  73. console.log("websocket连接错误:\n" + e);
  74. }
  75. console.log("websocket");
  76. }
  77. /* 连接 */
  78. connect() {
  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. console.log(`注册消息${this.settings}`)
  89. for (var index in this.settings) {
  90. this.client.subscribe(this.settings[index], frame => this.onmessage(frame));
  91. }
  92. }
  93. /* 发送 */
  94. send(destination, headers, body) {
  95. if (this.client.connected) {
  96. this.client.send(destination, headers, body);
  97. }
  98. }
  99. /* 发生错误 */
  100. onerror(error) {
  101. console.log(`websocket [${this.url}] 连接出现错误:\n${error.message}`);
  102. // TODO 断线重连还有问题
  103. setTimeout(this.connect(), 5000);
  104. }
  105. }