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用來(lái)通信
導(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() {
//注冊(cè)監(jiān)聽器用來(lái)接受消息
eb.registerHandler('some-address', function(error, message) {
console.log('received a message: ' + JSON.stringify(message));
});
//發(fā)送消息
eb.send('some-address', {name: 'tim', age: 587});
}
更多信息請(qǐng)參考這里 https://vertx.io/docs/vertx-web/java/
注:
對(duì)于需要發(fā)送消息來(lái)接受的消息,需要先監(jiān)聽,然后再發(fā)送消息。
對(duì)于一直推送的消息,不需要發(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。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue中使用的EventBus有生命周期
- Vue EventBus自定義組件事件傳遞
- vue采用EventBus實(shí)現(xiàn)跨組件通信及注意事項(xiàng)小結(jié)
- vue 使用eventBus實(shí)現(xiàn)同級(jí)組件的通訊
- 詳解EventBus 3.x 的快速使用
- vue中eventbus被多次觸發(fā)以及踩過(guò)的坑
- android事件總線EventBus3.0使用方法詳解
- Android EventBus(普通事件/粘性事件)詳解
- Android實(shí)現(xiàn)EventBus登錄界面與傳值(粘性事件)
- 詳解vue 組件之間使用eventbus傳值
相關(guān)文章
使用Angular內(nèi)置模塊進(jìn)行HTTP請(qǐng)求
這篇文章主要介紹了使用Angular內(nèi)置模塊進(jìn)行HTTP請(qǐng)求方法步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
詳解angular分頁(yè)插件tm.pagination二次觸發(fā)問(wèn)題解決方案
這篇文章主要介紹了詳解angular分頁(yè)插件tm.pagination二次觸發(fā)問(wèn)題解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
angular4 獲取wifi列表中文顯示亂碼問(wèn)題的解決
這篇文章主要介紹了angular4 獲取wifi列表中文顯示亂碼問(wèn)題的解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
AngularJS基礎(chǔ)學(xué)習(xí)筆記之指令
指令(Directives)是所有AngularJS應(yīng)用最重要的部分。盡管AngularJS已經(jīng)提供了非常豐富的指令,但還是經(jīng)常需要?jiǎng)?chuàng)建應(yīng)用特定的指令。這篇教程會(huì)為你講述如何自定義指令,以及介紹如何在實(shí)際項(xiàng)目中使用。2015-05-05
angular route中使用resolve在uglify壓縮后問(wèn)題解決
這篇文章主要介紹了angular route中使用resolve在uglify壓縮后問(wèn)題解決的相關(guān)資料,需要的朋友可以參考下2016-09-09
AngularJS基礎(chǔ) ng-cut 指令介紹及簡(jiǎn)單示例
本文主要介紹AngularJS ng-cut 指令,這里對(duì)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

