如何保持 node.js 和 python 之间的连接状态?
How to keep connectivity status between node.js and python?
我有基于 Node.js 和 python 硬件的 Web 应用程序。我想保持网络服务器和硬件之间的连接状态。如果硬件与 Web 应用程序断开连接,则 Web 应用程序应该收到事件或通知,因此我可以基于此向用户发送通知。我使用 mqtt 进行数据通信,但为了保持连接状态,我不能使用 MQTT,因为它与代理连接。我不想增加服务器的负载。
我应该使用哪个 tools/technology/protocol/method 来保持设备离线或在线的连接状态?。我还想在用户尝试使用 Web 应用程序将数据发送到硬件时使用,如果设备未与服务器连接,则用户应该根据连接状态收到设备离线的通知。
以下代码演示了我在评论中暗示的过程。
LWT 功能告诉代理在 1.5 倍的保活期限内未能响应时发布一条消息,将客户端标记为离线。如果客户端完全断开连接,则需要将其自身标记为离线。客户端在连接到代理时将其自身标记为在线。
所有状态消息都设置了保留位,因此当客户端订阅状态主题时,它们将始终被传递。
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("some/application/topic")
# set status message to online
client.publish("status/client1", payload="online", retain=True)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
if str(msg.payload) == "shutdown":
# update status to offline as this will be a clean dissconect
client.publish("status/client1", payload="offline", retain=True)
client.disconnect()
client = mqtt.Client(client_id="client1")
client.on_connect = on_connect
client.on_message = on_message
client.will_set("status/client1", payload="offline", retain=True)
client.connect("mqtt.eclipse.org", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
问题末尾请求的向离线客户端发送消息的通知将由OP执行。
我有基于 Node.js 和 python 硬件的 Web 应用程序。我想保持网络服务器和硬件之间的连接状态。如果硬件与 Web 应用程序断开连接,则 Web 应用程序应该收到事件或通知,因此我可以基于此向用户发送通知。我使用 mqtt 进行数据通信,但为了保持连接状态,我不能使用 MQTT,因为它与代理连接。我不想增加服务器的负载。
我应该使用哪个 tools/technology/protocol/method 来保持设备离线或在线的连接状态?。我还想在用户尝试使用 Web 应用程序将数据发送到硬件时使用,如果设备未与服务器连接,则用户应该根据连接状态收到设备离线的通知。
以下代码演示了我在评论中暗示的过程。
LWT 功能告诉代理在 1.5 倍的保活期限内未能响应时发布一条消息,将客户端标记为离线。如果客户端完全断开连接,则需要将其自身标记为离线。客户端在连接到代理时将其自身标记为在线。
所有状态消息都设置了保留位,因此当客户端订阅状态主题时,它们将始终被传递。
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("some/application/topic")
# set status message to online
client.publish("status/client1", payload="online", retain=True)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
if str(msg.payload) == "shutdown":
# update status to offline as this will be a clean dissconect
client.publish("status/client1", payload="offline", retain=True)
client.disconnect()
client = mqtt.Client(client_id="client1")
client.on_connect = on_connect
client.on_message = on_message
client.will_set("status/client1", payload="offline", retain=True)
client.connect("mqtt.eclipse.org", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
问题末尾请求的向离线客户端发送消息的通知将由OP执行。