فهرست منبع

Merge branch 'master' of http://49.4.49.126:3000/GYEE_R.D/neic

xushili 3 سال پیش
والد
کامیت
7ee831e51a
3فایلهای تغییر یافته به همراه110 افزوده شده و 36 حذف شده
  1. 14 14
      package-lock.json
  2. 18 5
      src/App.vue
  3. 78 17
      src/assets/script/MessageBridge.js

+ 14 - 14
package-lock.json

@@ -8778,8 +8778,8 @@
     },
     "eventsource": {
       "version": "1.1.0",
-      "resolved": "https://registry.npm.taobao.org/eventsource/download/eventsource-1.1.0.tgz?cache=0&sync_timestamp=1616041700200&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Feventsource%2Fdownload%2Feventsource-1.1.0.tgz",
-      "integrity": "sha1-AOjKfJIQnpSw3fMtrGd9hBAoz68=",
+      "resolved": "https://registry.npmjs.org/eventsource/-/eventsource-1.1.0.tgz",
+      "integrity": "sha512-VSJjT5oCNrFvCS6igjzPAt5hBzQ2qPBFIbJ03zLI9SE0mxwZpMw6BfJrbFHm1a141AavMEB8JHmBhWAd66PfCg==",
       "dev": true,
       "requires": {
         "original": "^1.0.0"
@@ -11364,8 +11364,8 @@
     },
     "json3": {
       "version": "3.3.3",
-      "resolved": "https://registry.npm.taobao.org/json3/download/json3-3.3.3.tgz",
-      "integrity": "sha1-f8EON1/FrkLEcFpcwKpvYr4wW4E=",
+      "resolved": "https://registry.npmjs.org/json3/-/json3-3.3.3.tgz",
+      "integrity": "sha512-c7/8mbUsKigAbLkD5B010BK4D9LZm7A1pNItkEwiUZRpIN66exu/e7YQWysGun+TRKaJp8MhemM+VkfWv42aCA==",
       "dev": true
     },
     "json5": {
@@ -13065,8 +13065,8 @@
     },
     "original": {
       "version": "1.0.2",
-      "resolved": "https://registry.npm.taobao.org/original/download/original-1.0.2.tgz",
-      "integrity": "sha1-5EKmHP/hxf0gpl8yYcJmY7MD8l8=",
+      "resolved": "https://registry.npmjs.org/original/-/original-1.0.2.tgz",
+      "integrity": "sha512-hyBVl6iqqUOJ8FqRe+l/gS8H+kKYjrEndd5Pm1MfBtsEKA038HkkdbAl/72EAXGyonD/PFsvmVG+EvcIpliMBg==",
       "dev": true,
       "requires": {
         "url-parse": "^1.4.3"
@@ -14396,8 +14396,8 @@
     },
     "querystringify": {
       "version": "2.2.0",
-      "resolved": "https://registry.npm.taobao.org/querystringify/download/querystringify-2.2.0.tgz",
-      "integrity": "sha1-M0WUG0FTy50ILY7uTNogFqmu9/Y=",
+      "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
+      "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==",
       "dev": true
     },
     "quick-lru": {
@@ -15663,8 +15663,8 @@
     },
     "sockjs-client": {
       "version": "1.5.1",
-      "resolved": "https://registry.npm.taobao.org/sockjs-client/download/sockjs-client-1.5.1.tgz",
-      "integrity": "sha1-JWkI9tWt+5Tau9vQLGY2LMoPnqY=",
+      "resolved": "https://registry.npmjs.org/sockjs-client/-/sockjs-client-1.5.1.tgz",
+      "integrity": "sha512-VnVAb663fosipI/m6pqRXakEOw7nvd7TUgdr3PlR/8V2I95QIdwT8L4nMxhyU8SmDBHYXU1TOElaKOmKLfYzeQ==",
       "dev": true,
       "requires": {
         "debug": "^3.2.6",
@@ -15677,8 +15677,8 @@
       "dependencies": {
         "debug": {
           "version": "3.2.7",
-          "resolved": "https://registry.npm.taobao.org/debug/download/debug-3.2.7.tgz?cache=0&sync_timestamp=1607566512593&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fdebug%2Fdownload%2Fdebug-3.2.7.tgz",
-          "integrity": "sha1-clgLfpFF+zm2Z2+cXl+xALk0F5o=",
+          "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz",
+          "integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==",
           "dev": true,
           "requires": {
             "ms": "^2.1.1"
@@ -17156,8 +17156,8 @@
     },
     "url-parse": {
       "version": "1.5.1",
-      "resolved": "https://registry.npm.taobao.org/url-parse/download/url-parse-1.5.1.tgz?cache=0&sync_timestamp=1613659698159&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Furl-parse%2Fdownload%2Furl-parse-1.5.1.tgz",
-      "integrity": "sha1-1fqYkK+KXh8nSiyYN2UQ9kJfbjs=",
+      "resolved": "https://registry.npmjs.org/url-parse/-/url-parse-1.5.1.tgz",
+      "integrity": "sha512-HOfCOUJt7iSYzEx/UqgtwKRMC6EU91NFhsCHMv9oM03VJcVo2Qrp8T8kI9D7amFf1cu+/3CEhgb3rF9zL7k85Q==",
       "dev": true,
       "requires": {
         "querystringify": "^2.1.1",

+ 18 - 5
src/App.vue

@@ -17,14 +17,27 @@ export default {
     CenterPage,
     StatusBar,
   },
-  props: {
-    
-  },
+  props: {},
   created: function () {
-    var ms = new MessageBridge();
-    ms.onmessage=function(){};
+    initData();
   },
+  methods: {},
 };
+
+function initData() {
+  var mb = MessageBridge.getInstance();
+  var vs = [
+    { key: "/topic/pv", action: pvmessage },
+    { key: "/topic/windturbine", action: windturbineMessage },
+  ];
+  mb.register(vs);
+}
+function pvmessage(msg) {
+  console.log(msg);
+}
+function windturbineMessage(msg) {
+  console.log(msg);
+}
 </script>
 
 <style scoped>

+ 78 - 17
src/assets/script/MessageBridge.js

@@ -1,15 +1,65 @@
 import Stomp from 'stompjs'
 
 export default class MessageBridge {
-  websokcetCalc;
-  websocketAdapter;
+  observers;// 观察者
+
+  calcSocket;// 后台websocket
+  adapterSocket;// 适配器websocket
+
   constructor() {
-    this.websokcetCalc = new WebSocket(onmessage, "ws://192.168.10.18:8011/wisdom", ["/topic/suggestion", "/topic/sync-command-result", "/topic/fault-count",
+    this.register = this.register.bind(this);
+    this.unregister = this.unregister.bind(this);
+    this.onmessage = this.onmessage.bind(this);
+    this.getActions = this.getActions(this);
+
+    this.observers = new Array();
+
+    this.calcSocket = new WebSocket("ws://192.168.10.18:8099/wisdom_service", this.onmessage, ["/topic/suggestion", "/topic/sync-command-result", "/topic/fault-count",
       "/topic/alarm-count", "/topic/fault-popup", "/topic/popup-remove", "/topic/heartbeat-data"]);
-    //this.websocketAdapter=new WebSocket(onmessage,"")
+    this.adapterSocket = new WebSocket("ws://192.168.10.18:8011/wisdom", this.onmessage, ["/topic/windturbine", "/topic/pv"]);
   }
+
+  /* 单例 */
+  static getInstance() {
+    if (!MessageBridge.instance) {
+      MessageBridge.instance = new MessageBridge();
+    }
+    return MessageBridge.instance;
+  }
+
+  /* 获得消息 */
   onmessage(msg) {
-    console.log(msg);
+    if(msg.headers.data-type && msg.headers.data-type=="heartbeat"){
+      console.log("获得心跳包!");
+      return;
+    }
+    if (msg.command != "MESSAGE" || !msg.headers.destination) return;
+    var os = this.getActions(msg.headers.destination);
+    for (var id in os) {
+      os[id].action(msg.body);
+    }
+  }
+
+  getActions(destination){
+    var list = new Array();
+    for(var i in this.observers){
+      if(this.observers[i].key==destination){
+        list.push(this.observers[i]);
+      }
+    }
+    return list;
+  }
+
+  /* 注册消息 */
+  register(msgs) {
+    for(var i in msgs){
+      this.observers.push(msgs[i]);
+    }
+  }
+
+  /* 取消注册消息 */
+  unregister(msgs) {
+    this.observers.remove(msgs);
   }
 }
 
@@ -18,45 +68,56 @@ class WebSocket {
   url;
   settings;
   client;
-  constructor(onmessage, url, settings) {
+  constructor(url, onmessage, settings) {
+    this.onerror = this.onerror.bind(this);
+    this.connectCallBackSubscribe = this.connectCallBackSubscribe.bind(this);
+    this.send = this.send.bind(this);
+
     this.onmessage = onmessage;
     this.url = url;
     this.settings = settings;
     try {
       this.connect();
     } catch (e) {
-      console.log("websocket连接错误:" + e);
+      console.log("websocket连接错误:\n" + e);
     }
 
     console.log("websocket");
   }
 
+  /* 连接 */
   connect() {
     if (this.client != null) {
       this.client.close();
     }
+    console.log(`正在连接websocket [${this.url}]`)
     this.client = Stomp.client(this.url);
-    this.client.connect({}, this.connectCallBackSubscribe, this.reconnect);
+    this.client.connect("", "", this.connectCallBackSubscribe, this.onerror);
+  }
+
+  /* 检测连接是否正常 */
+  protector() {
+
   }
 
   /* 注册 */
   connectCallBackSubscribe() {
-    if (this.settings == null || this.settings.length <= 0) return;
-    for (var st in this.settings) {
-      this.client.subscribe(st, frame => this.onmessage(frame));// 启停推荐
+    for (var index in this.settings) {
+      this.client.subscribe(this.settings[index], frame => this.onmessage(frame));
     }
   }
 
   /* 发送 */
   send(destination, headers, body) {
-    if (this.calcClient && this.calcClient.connected) {
-      this.calcClient.send(destination, headers, body);
+    if (this.client.connected) {
+      this.client.send(destination, headers, body);
     }
   }
 
-  /* 重新连接 */
-  reconnect() {
-    this.calcClient.connected = false;
-    clearTimeout(setTimeout(this.connect(), 5000));
+  /* 发生错误 */
+  onerror(error) {
+    console.log(`websocket [${this.url}] 连接出现错误:\n${error.message}`);
+    // TODO 断线重连还有问题
+    setTimeout(this.connect(), 5000);
   }
 }