Angular5集成eventbus的示例代碼
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)建一個(gè)eventbus.service.ts用來通信
導(dǎo)入eventbus:
import { EventBus } from 'vertx3-eventbus-client';
聲明eventbus:
declare var EventBus: any;
4.創(chuàng)建eventbus實(shí)例,監(jiān)聽接口以及發(fā)送消息
//創(chuàng)建實(shí)例
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ā)送。
代碼實(shí)例如下:
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;
}
在組件ngOnDestroy中調(diào)用closeEventBus關(guān)閉eventbus。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Angular內(nèi)置模塊進(jìn)行HTTP請求
這篇文章主要介紹了使用Angular內(nèi)置模塊進(jìn)行HTTP請求方法步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案
這篇文章主要介紹了詳解angular分頁插件tm.pagination二次觸發(fā)問題解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
AngularJS基礎(chǔ)學(xué)習(xí)筆記之指令
指令(Directives)是所有AngularJS應(yīng)用最重要的部分。盡管AngularJS已經(jīng)提供了非常豐富的指令,但還是經(jīng)常需要創(chuàng)建應(yīng)用特定的指令。這篇教程會為你講述如何自定義指令,以及介紹如何在實(shí)際項(xiàng)目中使用。2015-05-05
angular route中使用resolve在uglify壓縮后問題解決
這篇文章主要介紹了angular route中使用resolve在uglify壓縮后問題解決的相關(guān)資料,需要的朋友可以參考下2016-09-09
AngularJS基礎(chǔ) ng-cut 指令介紹及簡單示例
本文主要介紹AngularJS ng-cut 指令,這里對ng-cut指令的基礎(chǔ)資料進(jìn)行了整理,和詳細(xì)介紹,并附上代碼示例和實(shí)現(xiàn)效果圖,學(xué)習(xí)AngularJS 指令的朋友可以參考下2016-08-08
AngularJS中$apply方法和$watch方法用法總結(jié)
這篇文章主要介紹了AngularJS中$apply方法和$watch方法用法,結(jié)合實(shí)例形式總結(jié)分析了$apply方法和$watch方法的功能、參數(shù)含義、使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-12-12
Angular實(shí)現(xiàn)的table表格排序功能完整示例
這篇文章主要介紹了Angular實(shí)現(xiàn)的table表格排序功能,結(jié)合完整實(shí)例形式分析了AngularJS表格排序所涉及的事件響應(yīng)、元素遍歷、屬性修改等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12

