0

我正在尝试让 Aedes 充当 MQTT 代理和 Websocket 服务器。根据该文档:https ://github.com/moscajs/aedes/blob/master/docs/Examples.md 我想了解的内容。理想情况下,无论是 websocket 客户端还是 mqtt 客户端,我都希望监听器启动。

是否可以执行以下操作:

server.broadcast('foo/bar', {data:''})

并且所有客户端、websockets 和 mqtt 都会收到消息?该文档不是很清楚,我对使用 websocket-stream 感到非常惊讶。lvl很低吧?

这里有一些服务器端代码:

    const port = 1883

    const aedes = require('aedes')({

        persistence: mongoPersistence({
            url: 'mongodb://127.0.0.1/aedes-test',
            // Optional ttl settings
            ttl: {
                packets: 300, // Number of seconds
                subscriptions: 300
            }
        }),
        authenticate: (client, username, password, callback) => {



        },
        authorizePublish: (client, packet, callback) => {

        },

        authorizeSubscribe: (client, packet, callback) => {

        }
    });

    //const server = require('net').createServer(aedes.handle);
    const httpServer = require('http').createServer()
    const ws = require('websocket-stream')
    ws.createServer({ server: httpServer }, aedes.handle)


    httpServer.listen(port, function () {
        Logger.debug('Aedes listening on port: ' + port)
        aedes.publish({ topic: 'aedes/hello', payload: "I'm broker " + aedes.id })
    });
4

1 回答 1

2

它应该只是使用相同aedes对象启动两个服务器的情况,如下所示:

const port = 1883
const wsPort = 8883

const aedes = require('aedes')({

    persistence: mongoPersistence({
        url: 'mongodb://127.0.0.1/aedes-test',
        // Optional ttl settings
        ttl: {
            packets: 300, // Number of seconds
            subscriptions: 300
        }
    }),
    authenticate: (client, username, password, callback) => {



    },
    authorizePublish: (client, packet, callback) => {

    },

    authorizeSubscribe: (client, packet, callback) => {

    }
});

const server = require('net').createServer(aedes.handle);
const httpServer = require('http').createServer()
const ws = require('websocket-stream')
ws.createServer({ server: httpServer }, aedes.handle)

server.listen(port, function() {
    Logger.debug('Ades MQTT listening on port: ' + port)
})

httpServer.listen(wsPort, function () {
    Logger.debug('Aedes MQTT-WS listening on port: ' + wsPort)
    aedes.publish({ topic: 'aedes/hello', payload: "I'm broker " + aedes.id })
});
于 2020-03-12T11:54:53.300 回答