MQTTnet常见问题(一)-ReturnCode与ReasonCode

更新时间:2023-06-14 10:23:33 阅读: 评论:0

塞缪尔亨廷顿
MQTTnet常见问题(⼀)-ReturnCode与ReasonCode
⽬录
感统教育⽂章⽬录
什么是 “ReturnCode 已过时”
警告:“MqttConnectionValidatorContext.ReturnCode”已过时:“U ReasonCode instead. It is MQTTv5 only but will be converted to a valid ReturnCode.”
出现这个提⽰的原因是MQTT V5.0 版协议更新了标记返回状态的标识符。
在MQTTnet中的表现则是弃⽤了MQTT连接时标志连接状态(返回码)的⼀个枚举类型
MqttConnectReturnCode,改由描述更加全⾯的枚举类型MqttConnectReasonCode代替,新的返回原因码类型中包含了更加具体的状态标记。
我们可以看⼀下MQTT V5.0版协议中对返回原因码的定义
解决⽅案
虽然此警告置之不理并不会导致程序的异常(MQTTnet已内置了不同版本下返回标识的转换),但若是能获取到更加详尽的状态信息为什么要拒绝呢?
在服务端后返回状态码时采⽤ReasonCode,参考附录中的MqttConnectReasonCode源码注释,返回对应的状态信息。
附:源码
枚举类型MqttConnectReasonCode的源码
namespace MQTTnet.Protocol
{
public enum MqttConnectReasonCode
{
Success =0,// 成功
UnspecifiedError =128,// 未知的错误
MalformedPacket =129,// 数据包缺失
ProtocolError =130,// 协议有误
ImplementationSpecificError =131,// 实现细节有误
UnsupportedProtocolVersion =132,// 协议版本不⽀持
ClientIdentifierNotValid =133,// 客户端标识码⽆效
BadUrNameOrPassword =134,// 错误的⽤户名或密码
NotAuthorized =135,// 未授权
ServerUnavailable =136,// 服务器不可⽤
革命先烈的英雄事迹ServerBusy =137,// 服务器正忙
Banned =138,// 已禁⽤
BadAuthenticationMethod =140,// 验证⽅法有误
TopicNameInvalid =144,// ⽆效的Topic名称
PacketTooLarge =149,// 数据包过⼤
QuotaExceeded =151,// 超出配额
PayloadFormatInvalid =153,// Payload格式有误
RetainNotSupported =154,// 不⽀持保留
QoSNotSupported =155,// 不⽀持QoS
UAnotherServer =156,// 使⽤其他服务器
ServerMoved =157,// 服务器移动
ConnectionRateExceeded =159// 连接速率超出限定
我国最大的盐场}
}
枚举类型MqttConnectReturnCode的源码
namespace MQTTnet.Protocol
{
public enum MqttConnectReturnCode
{
ConnectionAccepted =0x00,
ConnectionRefudUnacceptableProtocolVersion =0x01,
ConnectionRefudIdentifierRejected =0x02,
ConnectionRefudServerUnavailable =0x03,楼宇电视
ConnectionRefudBadUrnameOrPassword =0x04,
ConnectionRefudNotAuthorized =0x05
}
}
⽤于转换MqttConnectReasonCodeConverter的源码
public class MqttConnectReasonCodeConverter
{
public MqttConnectReturnCode ToConnectReturnCode(MqttConnectReasonCode reasonCode) {
switch(reasonCode)
{
ca MqttConnectReasonCode.Success:
{
return MqttConnectReturnCode.ConnectionAccepted;
}
ca MqttConnectReasonCode.NotAuthorized:
{
return MqttConnectReturnCode.ConnectionRefudNotAuthorized;
return MqttConnectReturnCode.ConnectionRefudNotAuthorized;
神采飞扬的反义词}
ca MqttConnectReasonCode.BadUrNameOrPassword:
{
return MqttConnectReturnCode.ConnectionRefudBadUrnameOrPassword;
}
ca MqttConnectReasonCode.ClientIdentifierNotValid:
{
return MqttConnectReturnCode.ConnectionRefudIdentifierRejected;
}
ca MqttConnectReasonCode.UnsupportedProtocolVersion:
{
return MqttConnectReturnCode.ConnectionRefudUnacceptableProtocolVersion;
}
ca MqttConnectReasonCode.ServerUnavailable:
ca MqttConnectReasonCode.ServerBusy:
ca MqttConnectReasonCode.ServerMoved:
{
return MqttConnectReturnCode.ConnectionRefudServerUnavailable;
}
default:
{
throw new MqttProtocolViolationException("Unable to convert connect reason code (MQTTv5) to return code (MQTTv3).");
}
}
}
public MqttConnectReasonCode ToConnectReasonCode(MqttConnectReturnCode returnCode)
{
switch(returnCode)
{
ca MqttConnectReturnCode.ConnectionAccepted:
{
return MqttConnectReasonCode.Success;
}
ca MqttConnectReturnCode.ConnectionRefudUnacceptableProtocolVersion:
{
return MqttConnectReasonCode.UnsupportedProtocolVersion;
}
ca MqttConnectReturnCode.ConnectionRefudBadUrnameOrPassword:
{
return MqttConnectReasonCode.BadUrNameOrPassword;
}
ca MqttConnectReturnCode.ConnectionRefudIdentifierRejected:
{
return MqttConnectReasonCode.ClientIdentifierNotValid;
}
ca MqttConnectReturnCode.ConnectionRefudServerUnavailable:
{
return MqttConnectReasonCode.ServerUnavailable;
}
ca MqttConnectReturnCode.ConnectionRefudNotAuthorized:
{
return MqttConnectReasonCode.NotAuthorized;
防水卷材施工规范
}
dorkdefault:
{
throw new MqttProtocolViolationException("Unable to convert connect reason code (MQTTv5) to return code (MQTTv3).");
}
}
}
}

本文发布于:2023-06-14 10:23:33,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/951814.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:返回   状态   连接   有误   源码   服务器   协议   错误
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图