|
@@ -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);
|
|
|
}
|
|
|
}
|