成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

eventbus如何在Angular5中使用-創(chuàng)新互聯(lián)

這篇文章將為大家詳細講解有關eventbus如何在Angular5中使用,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

創(chuàng)新互聯(lián)公司成立于2013年,我們提供高端網站建設、成都網站制作、成都網站設計、網站定制、成都營銷網站建設小程序設計、微信公眾號開發(fā)、成都網站營銷服務,提供專業(yè)營銷思路、內容策劃、視覺設計、程序開發(fā)來完成項目落地,為成都OPP膠袋企業(yè)提供源源不斷的流量和訂單咨詢。

1.package.json中

"dependencies": {
   ...
  "vertx3-eventbus-client": "3.5.2",
 },

然后  npm install,或者:

npm install vertx3-eventbus-client@3.5.2

2.angular-cli.json中

 "scripts": [
     ...
    "../node_modules/vertx3-eventbus-client/vertx-eventbus.js"
   ],

3.創(chuàng)建一個eventbus.service.ts用來通信

導入eventbus:

import { EventBus } from 'vertx3-eventbus-client';

聲明eventbus:

declare var EventBus: any;

4.創(chuàng)建eventbus實例,監(jiān)聽接口以及發(fā)送消息

//創(chuàng)建實例
var eb = new EventBus('http://localhost:8080/eventbus');

eb.onopen = function() {
 //注冊監(jiān)聽器用來接受消息
 eb.registerHandler('some-address', function(error, message) {
  console.log('received a message: ' + JSON.stringify(message));
 });

 //發(fā)送消息
 eb.send('some-address', {name: 'tim', age: 587});

}

更多信息請參考這里 https://vertx.io/docs/vertx-web/java/

注:

對于需要發(fā)送消息來接受的消息,需要先監(jiān)聽,然后再發(fā)送消息。
對于一直推送的消息,不需要發(fā)送。

代碼實例如下:

RegisterHandler(key, id, callback) {
    const address = '***.' + key + '.' + id;
    if (typeof (this.eventBus[key]) === 'undefined' || !this.eventBus[key]) {
      this.eventBus[key] = new EventBus(environment.eventbusUrl);
    }
    if (this.eventBus[key].state === EventBus.OPEN) {
      this.eventBus[key].registerHandler(address, callback);
    } else {
      const $this = this;
      this.eventBus[key].onopen = function () {
        $this.eventBus[key].registerHandler(address, callback)
      }
    }
  }

Send(key, id) {
    var data = '';
    const address = ***.' + key + '.' + id;
    if (typeof (this.eventBus[key]) === 'undefined' || !this.eventBus[key]) {
      this.eventBus[key] = new EventBus(environment.eventbusUrl);
    }
    if (this.eventBus[key].state === EventBus.OPEN) {
      this.eventBus[key].send(address, data)
    } else {
      const $this = this;
      this.eventBus[key].onopen = function () {
        $this.eventBus[key].send(address, data)
      }
    }
  }

closeEventBus(key) {
    if (typeof (this.eventBus[key]) !== 'undefined' && this.eventBus[key] && this.eventBus[key].state === EventBus.OPEN) {
      this.eventBus[key].close();
    }
    this.eventBus[key] = null;
}

關于eventbus如何在Angular5中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

分享題目:eventbus如何在Angular5中使用-創(chuàng)新互聯(lián)
鏈接地址:http://jinyejixie.com/article26/eigjg.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網站建設、響應式網站、電子商務、定制開發(fā)Google

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網站建設