public void Publish(string topic, byte[] payload, MqttQosLevel qosLevel) { var deviceMessage = new DeviceMessage { Topic = topic, Payload = payload, QosLevel = qosLevel }; MessageReceived?.Invoke(this, new DeviceMessageReceivedEventArgs(deviceMessage)); }
public void Publish(string topic, byte[] payload, MqttQosLevel qosLevel) { try { var message = new MqttApplicationMessage(topic, payload, (MqttQualityOfServiceLevel)qosLevel, false); _client.PublishAsync(message).Wait(); _log.Verbose($"Published message '{topic}' [{Encoding.UTF8.GetString(payload)}]."); } catch (Exception exception) { _log.Error(exception, $"Failed to publish message '{topic}' [{Encoding.UTF8.GetString(payload)}]."); } }
public static void Publish(this IDeviceMessageBrokerService deviceMessageBrokerService, string topic, string message, MqttQosLevel qosLevel) { if (deviceMessageBrokerService == null) { throw new ArgumentNullException(nameof(deviceMessageBrokerService)); } if (topic == null) { throw new ArgumentNullException(nameof(topic)); } if (message == null) { throw new ArgumentNullException(nameof(message)); } deviceMessageBrokerService.Publish(topic, Encoding.UTF8.GetBytes(message), qosLevel); }
public void Publish(string topic, byte[] payload, MqttQosLevel qosLevel, bool retain) { try { var message = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(payload) .WithQualityOfServiceLevel((MqttQualityOfServiceLevel)qosLevel) .WithRetainFlag(retain) .Build(); _server.PublishAsync(message).GetAwaiter().GetResult(); _log.Verbose($"Published message '{topic}' [{Encoding.UTF8.GetString(payload)}]."); } catch (Exception exception) { _log.Error(exception, $"Failed to publish message '{topic}' [{Encoding.UTF8.GetString(payload)}]."); } }
protected internal override Task SubscribeAsync(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce) { return(Task.Factory.StartNew(() => Subscribe(topic, qos))); }
protected internal override void Subscribe(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce) { mqttClient.Subscribe(new string[] { topic }, new byte[] { (byte)qos }); }
public static void Subscribe(this MysoftMqttClient client, IMqttTopic topic, MqttQosLevel qos) { client.Subscribe(topic.Build(), qos); }
public TopicSubscriber(IMqttTopic topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce, bool shared = false) : this(topic.Build(), qos, shared) { }
public TopicSubscriber(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce, bool shared = false) { this.Topic = topic; this.Qos = qos; this.Shared = shared; }
public MqttMessageBuilder WithQos(MqttQosLevel qos) { _qos = qos; return(this); }
public static void Publish(this IDeviceMessageBrokerService deviceMessageBrokerService, string topic, string payload, MqttQosLevel qosLevel, bool retain) { if (deviceMessageBrokerService == null) { throw new ArgumentNullException(nameof(deviceMessageBrokerService)); } if (topic == null) { throw new ArgumentNullException(nameof(topic)); } if (payload == null) { throw new ArgumentNullException(nameof(payload)); } deviceMessageBrokerService.Publish(topic, Encoding.UTF8.GetBytes(payload), qosLevel, retain); }
public static void Publish(this IDeviceMessageBrokerService deviceMessageBrokerService, string topic, JObject payload, MqttQosLevel qosLevel, bool retain) { if (deviceMessageBrokerService == null) { throw new ArgumentNullException(nameof(deviceMessageBrokerService)); } if (topic == null) { throw new ArgumentNullException(nameof(topic)); } if (payload == null) { throw new ArgumentNullException(nameof(payload)); } deviceMessageBrokerService.Publish(topic, payload.ToString(Formatting.None), qosLevel, retain); }
/// <summary> /// 异步订阅 /// </summary> /// <param name="topic"></param> /// <param name="qos"></param> /// <returns></returns> protected internal abstract Task SubscribeAsync(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce);
/// <summary> /// 订阅主题 /// </summary> /// <param name="topic"></param> /// <param name="qos"></param> protected internal abstract void Subscribe(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce);
public void Publish(string topic, byte[] payload, MqttQosLevel qosLevel) { _client.PublishAsync(new MqttApplicationMessage(topic, payload, (MqttQualityOfServiceLevel)qosLevel, false)).Wait(); _log.Verbose($"Published message '{topic}' [{Encoding.UTF8.GetString(payload)}]."); }