MqttClientSubscribeMessageAsync 方法 (String) |
命名空间: HslCommunication.MQTT
// 简单的实例化例子 MqttClient mqttClient = new MqttClient( new MqttConnectionOptions( ) { ClientId = "ABC", // 客户端的唯一的ID信息 IpAddress = "127.0.0.1", // 服务器的地址 } );
// 订阅示例 mqttClient.OnMqttMessageReceived += ( MqttClient client, MqttApplicationMessage message ) => { Console.WriteLine( "Time:" + DateTime.Now.ToString( ) ); Console.WriteLine( "Topic:" + message.Topic ); Console.WriteLine( "Payload:" + Encoding.UTF8.GetString( message.Payload ) ); }; // 然后添加订阅 HslCommunication.OperateResult sub = mqttClient.SubscribeMessage( "A" ); if (sub.IsSuccess) { // 订阅成功 } else { // 订阅失败 }