123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import Stomp from 'stompjs'
- export default class MessageBridge {
- observers;// 观察者
- calcSocket;// 后台websocket
- adapterSocket;// 适配器websocket
- constructor() {
- 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.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) {
- 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);
- }
- }
- class WebSocket {
- onmessage;
- url;
- settings;
- client;
- 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连接错误:\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.onerror);
- }
- /* 检测连接是否正常 */
- protector() {
- }
- /* 注册 */
- connectCallBackSubscribe() {
- for (var index in this.settings) {
- this.client.subscribe(this.settings[index], frame => this.onmessage(frame));
- }
- }
- /* 发送 */
- send(destination, headers, body) {
- if (this.client.connected) {
- this.client.send(destination, headers, body);
- }
- }
- /* 发生错误 */
- onerror(error) {
- console.log(`websocket [${this.url}] 连接出现错误:\n${error.message}`);
- // TODO 断线重连还有问题
- setTimeout(this.connect(), 5000);
- }
- }
|