C#MQTTNET客户端使用说明

互联网 2021/9/14 1:05:01

C#MQTTNET客户端使用说明 1.如何使用1.我想启动MQTT客户端,我该怎么做?2.我想收到服务器发来消息,我该怎么做?3.我想知道客户端是否连接成功,我该怎么做?3.我想知道怎样重连服务器,我该怎么做?1.如何使用 代码github地址 1.我想启动MQTT客户端,我该怎么做?try{IMqttC…

C#MQTTNET客户端使用说明

    • 1.如何使用
      • 1.我想启动MQTT客户端,我该怎么做?
      • 2.我想收到服务器发来消息,我该怎么做?
      • 3.我想知道客户端是否连接成功,我该怎么做?
      • 3.我想知道怎样重连服务器,我该怎么做?

1.如何使用

代码github地址

1.我想启动MQTT客户端,我该怎么做?

  try
  {
   IMqttClient client = new MqttFactory().CreateMqttClient();
   var build = new MqttClientOptionsBuilder()
  //配置客户端Id
  .WithClientId(model.ClientId)
  //配置登录账号
  .WithCredentials("test","1234")
  //配置服务器IP端口 这里得端口号是可空的
  .WithTcpServer("192.168.0.1", "8080")
  //.WithTcpServer("www.baidu.com")这是一个例子
  ;
  //连接
  await client.ConnectAsync(build.Build());    
  } 
  catch (MqttConnectingFailedException) {
    Console.WriteLine("身份校验失败");
  }
 catch (Exception ex){
     var name = ex.GetType().FullName;
     Console.WriteLine("出现异常");
     Console.WriteLine(ex.Message);
}

2.我想收到服务器发来消息,我该怎么做?

client.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(MessageReceivedHandler);
/// <summary>
/// 收到消息
/// </summary>
/// <param name="obj"></param>
private void MessageReceivedHandler(MqttApplicationMessageReceivedEventArgs obj)
{
  Console.WriteLine("===================================================");
  Console.WriteLine("收到消息:");
  Console.WriteLine($"主题:{obj.ApplicationMessage.Topic}");
  Console.WriteLine($"消息:{Encoding.UTF8.GetString(obj.ApplicationMessage.Payload)}");
  Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++");
  Console.WriteLine();
}
client.UseApplicationMessageReceivedHandler(args=> {
   Console.WriteLine("===================================================");
   Console.WriteLine("收到消息:");
   Console.WriteLine($"主题:{args.ApplicationMessage.Topic}");
   Console.WriteLine($"消息:{Encoding.UTF8.GetString(args.ApplicationMessage.Payload)}");
   Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++");
   Console.WriteLine();
});

3.我想知道客户端是否连接成功,我该怎么做?

//连接成功 
client.ConnectedHandler = new MqttClientConnectedHandlerDelegate(ConnectedHandler);
/// <summary>
/// 连接成功 在此处做订阅主题(Topic)操作
/// </summary>
/// <param name="obj"></param>
private void ConnectedHandler(MqttClientConnectedEventArgs obj)
{
	Console.WriteLine("本客户端已连接成功");
	Console.WriteLine($"地址:{model.IP}");
	Console.WriteLine($"端口:{model.Port}");
	Console.WriteLine($"客户端:{model.ClientId}");
	Console.WriteLine($"账号:{model.Account}");
	Console.WriteLine();
	//第1种订阅方式
	client.SubscribeAsync("主题名称").GetAwaiter().GetResult();

	//第2种订阅方式
	List<MqttTopicFilter> Topics = new List<MqttTopicFilter>();
	Topics.Add(new MqttTopicFilter() { Topic= "主题名称A", QualityOfServiceLevel= MqttQualityOfServiceLevel.ExactlyOnce});
	Topics.Add(new MqttTopicFilter() { Topic = "主题名称B" });
	Topics.Add(new MqttTopicFilter() { Topic = "主题名称C" });
	client.SubscribeAsync(Topics.ToArray()).GetAwaiter().GetResult();

	//第3种订阅方式
	MqttClientSubscribeOptionsBuilder builder = new MqttClientSubscribeOptionsBuilder();
	builder.WithTopicFilter("AAA");
	client.SubscribeAsync(builder.Build()).GetAwaiter().GetResult();
}
client.UseConnectedHandler(args=> {
	Console.WriteLine("本客户端已连接成功");
	Console.WriteLine($"地址:{model.IP}");
	Console.WriteLine($"端口:{model.Port}");
	Console.WriteLine($"客户端:{model.ClientId}");
	Console.WriteLine($"账号:{model.Account}");
	Console.WriteLine();
	//第1种订阅方式
	client.SubscribeAsync("主题名称").GetAwaiter().GetResult();

	//第2种订阅方式
	List<MqttTopicFilter> Topics = new List<MqttTopicFilter>();
	Topics.Add(new MqttTopicFilter() { Topic = "主题名称A", QualityOfServiceLevel = MqttQualityOfServiceLevel.ExactlyOnce });
    Topics.Add(new MqttTopicFilter() { Topic = "主题名称B" });
    Topics.Add(new MqttTopicFilter() { Topic = "主题名称C" });
    client.SubscribeAsync(Topics.ToArray()).GetAwaiter().GetResult();

    //第3种订阅方式
    MqttClientSubscribeOptionsBuilder builder = new MqttClientSubscribeOptionsBuilder();
    builder.WithTopicFilter("AAA");
    client.SubscribeAsync(builder.Build()).GetAwaiter().GetResult();
});

3.我想知道怎样重连服务器,我该怎么做?

//断开连接 重连就写在此处
client.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(DisconnectedHandler);
/// <summary>
/// 客户端断开连接后,如果需要重连在此处实现
/// </summary>
/// <param name="obj"></param>
private void DisconnectedHandler(MqttClientDisconnectedEventArgs obj)
{
	Console.WriteLine("本客户端已经断开连接");
	Console.WriteLine();
	try
	{
		client.ConnectAsync(options).GetAwaiter().GetResult();
	}
	catch (Exception)
	{
		Console.WriteLine("重连失败");
	}
}
client.UseDisconnectedHandler(args =>
{
	Console.WriteLine("本客户端已经断开连接");
	Console.WriteLine();
	try
	{
		client.ConnectAsync(options).GetAwaiter().GetResult();
	}
	catch (Exception)
	{
		Console.WriteLine("重连失败");
	}
});
随时随地学软件编程-关注百度小程序和微信小程序
关于找一找教程网

本站文章仅代表作者观点,不代表本站立场,所有文章非营利性免费分享。
本站提供了软件编程、网站开发技术、服务器运维、人工智能等等IT技术文章,希望广大程序员努力学习,让我们用科技改变世界。
[C#MQTTNET客户端使用说明]http://www.zyiz.net/tech/detail-227824.html

上一篇:开源.net混淆器——ConfuserEx

下一篇:C#多线程开发-了解C#5.0 05

赞(0)
关注微信小程序
程序员编程王-随时随地学编程

扫描二维码或查找【程序员编程王】

可以随时随地学编程啦!

技术文章导航 更多>
扫一扫关注最新编程教程