我試着客戶端1和客戶端2程序,我可以很容易地與他們溝通。我可以輕鬆地發送消息並與他們一起接收消息,但我不知道一個客戶端是否斷開連接,如何將斷開的消息發送給訂閱的客戶端。如果一個客戶端在MQTT中斷開連接,如何將斷開連接消息發送給次目標客戶端?
客戶1:
var mqtt=require("mqtt");
var express=require("express");
var app=express();
var options={
keepalive:100,
port: 1883,
clientId:'1',
clientSession:false,
host: "http://localhost:8000",
will:
{
topic:'willMag',
payload:"connection closed abnormallly r",
qos:1,
retain:true
}
};
var client=mqtt.connect("tcp://192.168.43.137:1883",options);
client.on("connect",function()
{
setInterval(function()
{
client.publish("ranjith/princy","hello love you princy",function()
{
console.log("message published in client1");
});
},2000);
client.subscribe("bv/nivi");
client.on("message",function(topic,message)
{
console.log("I recieved the topic:"+topic);
console.log("I recieved the message:"+message);
});
});
client.on("disconnect",function()
{
console.log("disconnected client1");
});
app.listen(8000,function()
{
console.log("server listen at port 8000");
});
客戶2:
var mqtt=require("mqtt");
var express=require("express");
var app=express();
var options={
keepalive:100,
port: 1883,
clientId:'2',
clientSession:false,
host: "http://localhost:8086",
will:
{
topic:'willMag',
payload:"connection closed abnormallly b",
qos:1,
retain:true
}
};
var client=mqtt.connect("tcp://192.168.43.137:1883",options);
client.on("connect",function()
{
setInterval(function(){
client.publish("bv/nivi","hello love you nivi",function()
{
console.log("message published in client2");
});
},2000);
client.subscribe("ranjith/princy");
client.on("message",function(topic,message)
{
console.log("I recieved the topic:"+topic);
console.log("I recieved the message:"+message);
});
});
client.on("disconnect",function()
{
console.log("disconnected client2");
});
app.listen(8086,function()
{
console.log("server listen at port 8000");
});
您使用的是什麼經紀商?你有權訪問它的日誌文件嗎? – blp
請閱讀[在什麼情況下,我可以添加「緊急」或其他類似的短語到我的問題,以獲得更快的答案?](https://meta.stackoverflow.com/q/326569) - 總結是,這不是解決志願者問題的理想方式,而且可能對獲得答案起反作用。請不要將這添加到您的問題。 – halfer
我看到你正在操作[兩個堆棧溢出帳戶](https://stackoverflow.com/users/8109812/vignesh-ravi),Vignesh。請不要這樣做。如果您足夠低估了一個帳戶不再運作,那麼您需要提高您的問題或答案的質量,以便被允許再次發佈。此外,聽起來你需要耐心一些 - 在你的所有問題上加上「我正在等待」,不利於獲得答案。 – halfer