点击或拖拽改变大小

MqttClientSubscribeMessageAsync 方法 (String)

从服务器订阅一个或多个主题信息
Subscribe to one or more topics from the server

命名空间:  HslCommunication.MQTT
程序集:  HslCommunication (在 HslCommunication.dll 中) 版本:12.1.2.0 (12.1.2.0)
语法
public Task<OperateResult> SubscribeMessageAsync(
	string[] topics
)

参数

topics
类型:SystemString

[缺少 "M:HslCommunication.MQTT.MqttClient.SubscribeMessageAsync(System.String[])" 的 <param name="topics"/> 文档]

返回值

类型:TaskOperateResult
订阅结果
示例
参照 MqttClient 的示例说明。
简单的实例化
// 简单的实例化例子
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
{
    // 订阅失败
}
参见