MQTT(Message Queuing Telemetry Transport)
即时通讯协议,
开发商 IBM
MQTT(消息队列遥测传输)是ISO 标准(ISO/IEC PRF 20922)下基于发布/订阅范式的消息协议。它工作在 TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟糕的情况下而设计的发布/订阅型消息协议,为此,它需要一个消息中间件 。
MQTT是一个基于客户端-服务器的消息发布/订阅传输协议。MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛。在很多情况下,包括受限的环境中,如:机器与机器(M2M)通信和物联网(IoT)。其在,通过卫星链路通信传感器、偶尔拨号的医疗设备、智能家居、及一些小型化设备中已广泛使用。
新建Winform应用程序,将默认的Form1重命名为FormMqttServer
管理Nuget包,输入关键字Mqtt
点击安装
安装完成后示例如图
我们使用MQTTnet.dll,程序集 MQTTnet, Version=4.3.6.1152。应用程序.net framework版本需要4.6.1或者以上
窗体 FormMqttServer 设计如下:
窗体 FormMqttServer设计器代码如下:
文件 FormMqttServer.Designer.cs
namespace CultureDemo
{
partial class FormMqttServer
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lvTopic = new System.Windows.Forms.ListBox();
this.lbClients = new System.Windows.Forms.ListBox();
this.rtxtMessage = new System.Windows.Forms.RichTextBox();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.btnStop = new System.Windows.Forms.Button();
this.btnStart = new System.Windows.Forms.Button();
this.txtPassword = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.txtUsername = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.txtPort = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.txtIP = new System.Windows.Forms.TextBox();
this.lb = new System.Windows.Forms.Label();
this.dgvTopic = new System.Windows.Forms.DataGridView();
this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column4 = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.dgvTopic)).BeginInit();
this.SuspendLayout();
//
// lvTopic
//
this.lvTopic.FormattingEnabled = true;
this.lvTopic.ItemHeight = 12;
this.lvTopic.Location = new System.Drawing.Point(686, 35);
this.lvTopic.Name = "lvTopic";
this.lvTopic.Size = new System.Drawing.Size(300, 136);
this.lvTopic.TabIndex = 31;
//
// lbClients
//
this.lbClients.FormattingEnabled = true;
this.lbClients.I