npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dp-mq-client

v1.0.5

Published

javascript typescript ioc node.js

Downloads

5

Readme

dp-mq-client

。 简单易用的mqtt 客户端 库。

一、安装

  • npm i dp-ioc

二、快速开始

import { MqttClient, connect } from "mqtt";
import { DpMqClient, MqMessage } from "dp-mq-client"


//定义一个消息体
@MqMessage({
    topic: "a/b/c", // 消息主题
})
class TestMsg {
    constructor(
        public name: string,
        public age: number
    ) { }
}


// 初始化客户端
let mqttClient = connect("mqtt://127.0.0.1", { port: 10084, password: "xx", username: "xxx" });
mqttClient.on("connect", () => {
    let dpMq = new DpMqClient();
    dpMq.init(mqttClient);


    // 实例化消息体
    let testMsg = new TestMsg("rajan", 18);

    // 订阅消息
    dpMq.subscribe<TestMsg>(testMsg, (msg) => {
        // 当有消息时触发回调。
    })

    // 发布消息
    dpMq.publish(testMsg);

    // 取消订阅消息 
    dpMq.unsubscribe(testMsg);

})

注意,取消订阅时,需要传入消息体实例而非类

如果使用在dp-uni-api-framework 框架下,可以如此引入:

...
class App {

    @Inject(DpMqClient)
    dpMqClient:DpMqClient;

    @BeforeBoot
    async beforeBoot(){

        //  callback写法
        let mqttClient = connect("mqtt://127.0.0.1", { port: 10084, password: "xx", username: "xxx" });
        mqttClient.on("connect", () => {
            this.dpMqClient.init(mqttClient);
        })

        // 或者使用await写法 [推荐]
        await new Promise((resolve,reject)=>{
            let mqttClient = connect("mqtt://127.0.0.1", { port: 10084, password: "xx", username: "xxx" });
            mqttClient.on("connect", () => {
                this.dpMqClient.init(mqttClient);
                resolve();
            })
            mqttClient.on("error", () => {
                reject();
            })
        })
    }
}
...

三、高级用法

3.1 订阅消息时, 同一个消息体类多个实例对象,每个实例对象的消息回调是独立的。

@MqMessage({
    topic: "a/b/c", 
})
class TestMsg {
    constructor(
        public name: string,
        public age: number
    ) { }
}

//定义两个消息实例
let testMsg1 = new TestMsg("rajan1", 19);
let testMsg2 = new TestMsg("rajan2", 20);


// 订阅消息
dpMq.subscribe<TestMsg>(testMsg1, (msg) => {
     // 消息回调1 
})

// 订阅消息
dpMq.subscribe<TestMsg>(testMsg2, (msg) => {
     // 消息回调2
})

当然,取消订阅(unsubscribe)也是独立的。

3.2 动态更新 meta 元信息


import { metaUpdate,MqMessage } from "dp-mq-client"

@MqMessage({
    topic: "a/b/c", 
})
class TestMsg {
    constructor(
        public name: string,
        public age: number
    ) { }
}

let testMsg1 = new TestMsg("rajan1", 19);
metaUpdate(testMsg1,{ topic: "a/c/c"});// 更新testMsg1对象的元信息

四、支持

如果在使用过程中,遇到问题,请到git仓库反馈 仓库地址