1.引入MQTTnet.dll
本例引用版本为3.0.15.0,运行环境为.net Framework 4.6.1, 如版本更新,请参考最新官方文档:https://github.com/chkr1011/MQTTnet/wiki/
2.创建mqttServer
private IMqttServer MqttServer;
private MqttServerOptionsBuilder MqttServerOptions;
MqttServerOptions = new MqttServerOptionsBuilder()
.WithDefaultEndpointBoundIPAddress(IPAddress.Parse(“192.168.1.1”))//ip
.WithDefaultEndpointPort(1883) //端口号
.WithConnectionValidator(c =>
{
if (c.ClientId.Length < 10)
{
c.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
return;
}
if (c.Username != "Username") //用户名
{
c.ReasonC