我已经设置了一个运行 Mosca 的节点 js 服务器。客户端能够连接到 Mosca 服务器并发布消息。我需要以消息(订阅某个主题)的形式向客户端发送确认。
客户端发布消息后,以下代码会连续发送多条消息。我错过了什么吗?
var settings = {
port: 1882,
backend: ascoltatore
};
var message = {
topic: 'crofters',
payload: 'OK', // or a Buffer
qos: 2
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client ) {
var packet_payload = packet.payload;
packet_payload = packet_payload.toString();
console.log('Published', packet_payload);
server.publish(message, function() {
console.log('done!');
});
});
server.on('ready', setup);
function setup() {
console.log('Mosca server is up and running');
}