Angular5集成eventbus的示例代码

 更新时间:2018年07月19日 09:11:21   作者:是小太阳呀  
这篇文章主要介绍了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.创建一个eventbus.service.ts用来通信

导入eventbus:

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

声明eventbus:

declare var EventBus: any;

4.创建eventbus实例,监听接口以及发送消息

//创建实例
var eb = new EventBus('http://localhost:8080/eventbus');

eb.onopen = function() {
 //注册监听器用来接受消息
 eb.registerHandler('some-address', function(error, message) {
  console.log('received a message: ' + JSON.stringify(message));
 });

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

}

更多信息请参考这里 https://vertx.io/docs/vertx-web/java/

注:

对于需要发送消息来接受的消息,需要先监听,然后再发送消息。
对于一直推送的消息,不需要发送。

代码实例如下:

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中调用closeEventBus关闭eventbus。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 使用Angular内置模块进行HTTP请求

    使用Angular内置模块进行HTTP请求

    这篇文章主要介绍了使用Angular内置模块进行HTTP请求方法步骤详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • 详解angular分页插件tm.pagination二次触发问题解决方案

    详解angular分页插件tm.pagination二次触发问题解决方案

    这篇文章主要介绍了详解angular分页插件tm.pagination二次触发问题解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • Angular中的请求拦截的方法

    Angular中的请求拦截的方法

    我们的接口是单独编写的处理的,在实际的开发项目中,有众多的接口,有些需要登陆凭证,有些不需要。一个一个接口处理不妥,我们是否可以考虑对请求进行拦截封装,感兴趣的可以了解一下
    2022-04-04
  • angular4 获取wifi列表中文显示乱码问题的解决

    angular4 获取wifi列表中文显示乱码问题的解决

    这篇文章主要介绍了angular4 获取wifi列表中文显示乱码问题的解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • AngularJS入门教程之Scope(作用域)

    AngularJS入门教程之Scope(作用域)

    本文主要介绍AngularJS Scope(作用域),这里对Score知识做了详细介绍,并提供实例代码,有需要的小伙伴可以参考下
    2016-07-07
  • AngularJS基础学习笔记之指令

    AngularJS基础学习笔记之指令

    指令(Directives)是所有AngularJS应用最重要的部分。尽管AngularJS已经提供了非常丰富的指令,但还是经常需要创建应用特定的指令。这篇教程会为你讲述如何自定义指令,以及介绍如何在实际项目中使用。
    2015-05-05
  • angular route中使用resolve在uglify压缩后问题解决

    angular route中使用resolve在uglify压缩后问题解决

    这篇文章主要介绍了angular route中使用resolve在uglify压缩后问题解决的相关资料,需要的朋友可以参考下
    2016-09-09
  • AngularJS基础 ng-cut 指令介绍及简单示例

    AngularJS基础 ng-cut 指令介绍及简单示例

    本文主要介绍AngularJS ng-cut 指令,这里对ng-cut指令的基础资料进行了整理,和详细介绍,并附上代码示例和实现效果图,学习AngularJS 指令的朋友可以参考下
    2016-08-08
  • AngularJS中$apply方法和$watch方法用法总结

    AngularJS中$apply方法和$watch方法用法总结

    这篇文章主要介绍了AngularJS中$apply方法和$watch方法用法,结合实例形式总结分析了$apply方法和$watch方法的功能、参数含义、使用技巧与相关注意事项,需要的朋友可以参考下
    2016-12-12
  • Angular实现的table表格排序功能完整示例

    Angular实现的table表格排序功能完整示例

    这篇文章主要介绍了Angular实现的table表格排序功能,结合完整实例形式分析了AngularJS表格排序所涉及的事件响应、元素遍历、属性修改等相关操作技巧,需要的朋友可以参考下
    2017-12-12

最新评论