推送代理作为订阅者
默认情况下,推送代理可将数据发送到云。编辑推送代理代码,以便从云接收数据。
数据接收功能
每次接收到消息时都会调用以下示例中的函数。该函数根据函数中定义的逻辑来处理所收到的消息,然后更新
messageVariable
变量值。private Task SubscribeClientMqttMsgPublishReceived(MqttApplicationMessageReceivedEventArgs e) { var message = e.ApplicationMessage.ConvertPayloadToString(); var messageVariable = Project.Current.GetVariable("Model/Message"); messageVariable.Value = Newtonsoft.Json.Linq.JObject.Parse(message)["Rows"][0]["Variables"][0]["Value"].ToString(); return Task.CompletedTask; }
订阅者配置参数
以下示例显示作为订阅者的配置参数传递的数据接收函数。除发布服务器的基本配置外,还在
Start()
方法中配置订阅者方法。public override void Start() { // PushAgent Default configuration // Add subscriber mqttClientConnector.AddSubscriberAsync("my_custom_subscriber_topic", 1, SubscribeClientMqttMsgPublishReceived); } public async void AddSubscriberAsync(string topic, int qosLevel, Func<MqttApplicationMessageReceivedEventArgs, Task> subscribeClientMqttMsgPublishReceived) { mqttClient.ApplicationMessageReceivedAsync += subscribeClientMqttMsgPublishReceived; await mqttClient.SubscribeAsync(topic, GetQoSLevel(qosLevel)).ConfigureAwait(false); }
提供反馈