简体   繁体   中英

node mqtt server not propagating publishings

in this sample project i am trying to subscribe to a channel and publish into it.

However i never get back the message sent to the broker.

This is the server:

const mqttServer = require("mqtt-server");

const servers = mqttServer(
    {
        mqtt: "tcp://localhost:1883",
        mqttws: "ws://localhost:1884",
    },
    {
        emitEvents: true, // default
    },
    client => {
        console.log("client connected!");
        // console.log(client)
        client.connack({
            returnCode: 0,
        });
        client.on("data", data => {
      console.log("got data: ");
      console.log(data);
      client.puback({messageId:data.packetId, ...data})
    });


    client.on("error", err => {
      console.log(err);
    });
    },
);

servers.listen(_ => {
    console.log("listening!");
});

servers.close(_ => {
    console.log("close!");
});

And this is the client:

const mqtt = require("mqtt");

const client = mqtt.connect("mqtt://localhost:1883");

client.on("connect", ack => {
    console.log("connected!");
    // console.log(ack);
    client.subscribe("/test", err => {
        console.log(err);
    });

    client.on("message", (topic, message) => {
        console.log(topic);
        // message is Buffer
        console.log(message.toString());
    });

    setInterval(_ => {
        client.publish("/test", "Hello mqtt");
    }, 3000);
});

client.on("error", err => {
    console.log(err);
});

Any thoughts on what am i missing?

事实证明,使用伊迪斯经纪人,示例项目上的场景就很好了!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM