Documentation ¶
Index ¶
- Constants
- type AUTHProtocol
- type CONNACKProtocol
- type CONNECTProtocol
- type DISCONNECTProtocol
- type Fixed
- type ImplMqttProto
- type PINGREQProtocol
- type PINGRESPProtocol
- type PUBACKProtocol
- type PUBCOMPProtocol
- type PUBLISHProtocol
- type PUBRECProtocol
- type PUBRELProtocol
- type SUBACKProtocol
- type SUBSCRIBEProtocol
- type TopicFilter
- type UNSUBACKProtocol
- type UNSUBSCRIBEProtocol
- type Variable
- type Will
Constants ¶
const ( CONNECT = 0x10 // == 16 0001 0000 C=>S CONNACK = 0x20 // == 32 0010 0000 S=>C PUBLISH = 0x30 // == 48 0011 xxxx 大于等于48 小于 64 C<=>S PUBLISH31 = 0x31 // == 49 0011 0001 保留信息 C<=>S PUBLISH32 = 0x32 // == 50 0011 0010 需要 PUBACK C<=>S PUBLISH33 = 0x33 // == 51 0011 0011 需要 PUBACK 保留信息 C<=>S PUBLISH34 = 0x34 // == 52 0011 0100 Qos2 需要 PUBREC C<=>S PUBLISHMAX = 0x3D // == 61 订阅协议二进制转化 ,最大值 PUBACK = 0x40 // == 64 0100 0000 C<=>S PUBREC = 0x50 // == 80 0101 0000 C<=>S PUBREL = 0x62 // ==98 0110 0010 C<=>S PUBCOMP = 0x70 // ==112 0111 0000 C<=>S SUBSCRIBE = 0x82 // == 130 1000 0010 C=>S SUBACK = 0x90 // ==144 1001 0000 S=>C UNSUBSCRIBE = 0xA2 // == 162 1010 0010 C=>S UNSUBACK = 0xB0 // == 176 1011 0000 S=>C PINGREQ = 0xC0 // == 192 1100 0000 C=>S PINGRESP = 0xD0 // == 208 1101 0000 S=>C DISCONNECT = 0xE0 // == 224 1110 0000 C=>S AUTH = 0xF0 // == 240 1111 0000 C<=>S )
协议编号
const ( Success uint8 = 0 // Connection Accepted 链接已接受 Unspecified_error uint8 = 0x80 // 未指明的错误 服务端不愿透露的错误,或者没有适用的原因码。 Malformed_Packet uint8 = 0x81 // 无效报文 CONNECT报文内容不能被正确的解析。 Protocol_Error uint8 = 0x82 // 协议错误 CONNECT报文内容不符合本规范。 Implementation_s_e uint8 = 0x83 // 实现特定错误 CONNECT有效,但不被服务端所接受。 UnsupportedPV uint8 = 0x84 // 协议版本不支持 服务端不支持客户端所请求的MQTT协议版本 ClientInotv uint8 = 0x85 // 客户标识符无效 客户标识符有效,但未被服务端所接受。 BadUNorP uint8 = 0x86 //用户名密码错误 客户端指定的用户名密码未被服务端所接受。 Server_busy uint8 = 0x89 // 服务端正忙 服务端正忙,请重试。 Banned uint8 = 0x8A // 客户端被禁止,请联系服务端管理员。 Bad_a_method uint8 = 0x8C // 无效的认证方法 认证方法未被支持,或者不匹配当前使用的认证方法。 Topic_Name_invalid uint8 = 0x90 // 主题名无效 遗嘱主题格式正确,但未被服务端所接受。 Packet_too_large uint8 = 0x95 //报文过长 CONNECT报文超过最大允许长度。 Quota_exceeded uint8 = 0x97 // 超出配额 已超出实现限制或管理限制。 Payload_f_i uint8 = 0x99 // 载荷格式无效 遗嘱载荷数据与载荷格式指示符不匹配。 Retain_n_s uint8 = 0x9A //不支持保留 遗嘱保留标志被设置为1,但服务端不支持保留消息。 QoS_n_s uint8 = 0x9B //不支持的QoS等级 服务端不支持遗嘱中设置的QoS等级。 Use_a_s uint8 = 0x9C //(临时)使用其他服务端 客户端应该临时使用其他服务端。 Server_moved uint8 = 0x9D //服务端已(永久)移动 客户端应该永久使用其他服务端 Connection_r_e uint8 = 0x9F // 超出连接速率限制 超出了所能接受的连接速率限制。 // 以下是非connack 使用的 Disconnect_w_W_M uint8 = 0x04 // 包含遗嘱的断开 DISCONNECT No_m_subscribers uint8 = 0x10 // 无匹配订阅 PUBACK, PUBREC No_subscription_e uint8 = 0x11 //订阅不存在 UNSUBACK Continue_a uint8 = 0x18 //继续认证 AUTH Re_authenticate uint8 = 0x19 // 重新认证 AUTH Server_s_down uint8 = 0x8B //服务端关闭中 DISCONNECT Keep_Alive_to uint8 = 0x8D // 保活超时 DISCONNECT Session_to uint8 = 0x8E //会话被接管 DISCONNECT Topic_Filter_i uint8 = 0x8F // 主题过滤器无效 SUBACK, UNSUBACK, DISCONNECT Packet_Iinuse uint8 = 0x91 //报文标识符已被占用 PUBACK, PUBREC, SUBACK, UNSUBACK Packet_Inotf uint8 = 0x92 //报文标识符无效 PUBREL, PUBCOMP Receive_M_e uint8 = 0x93 //接收超出最大数量 DISCONNECT Topic_A_i uint8 = 0x94 // 主题别名无效 DISCONNECT Message_r_t_h uint8 = 0x96 //消息太过频繁 DISCONNECT Administrative_a uint8 = 0x98 //管理行为 DISCONNECT Maximum_c_t uint8 = 0xA0 //最大连接时间 Subscription_I_n_s uint8 = 0xA1 //不支持订阅标识符 SUBACK, DISCONNECT Wildcard_S_n_s uint8 = 0xA2 //不支持通配符订阅 SUBACK, DISCONNECT )
响应码
const ( QoS0 uint8 = iota //QoS = 0 – 最多发一次 QoS1 //QoS = 1 – 最少发一次 QoS2 //QoS = 2 – 保证收一次 )
const ( //UTF-8编码字符串 两个字节标识长度,后面的是内容 PayloadFI uint8 = 0x01 //Payload Format Indicator 载荷格式说明 字节 PUBLISH, Will Properties MessageEI uint8 = 0x02 //Message Expiry Interval 消息过期时间 四字节整数 PUBLISH, Will Properties ContentType uint8 = 0x03 //Content Type 内容类型 UTF-8编码字符串 PUBLISH, Will Properties ResponseTopic uint8 = 0x08 //Response Topic 响应主题 UTF-8编码字符串 PUBLISH, Will Properties CorrelationData uint8 = 0x09 //Correlation Data 相关数据 二进制数据 PUBLISH, Will Properties SubscriptionI uint8 = 0x0B //Subscription Identifier 定义标识符 变长字节整数 PUBLISH, SUBSCRIBE SessionEI uint8 = 0x11 //Session Expiry Interval 会话过期间隔 四字节整数 CONNECT, CONNACK, DISCONNECT AssignedCI uint8 = 0x12 //Assigned Client Identifier 分配客户标识符 UTF-8编码字符串 CONNACK ServerKA uint8 = 0x13 //Server Keep Alive 服务端保活时间 双字节整数 CONNACK AuthenticationM uint8 = 0x15 // Authentication Method 认证方法 UTF-8编码字符串 CONNECT, CONNACK, AUTH AuthenticationD uint8 = 0x16 //Authentication Data 认证数据 二进制数据 CONNECT, CONNACK, AUTH RequestPI uint8 = 0x17 //Request Problem Information 请求问题信息 字节 CONNECT WillDI uint8 = 0x18 // Will Delay Interval 遗嘱延时间隔 四字节整数 Will Properties RequestRI uint8 = 0x19 //Request Response Information 请求响应信息 字节 CONNECT ResponseI uint8 = 0x1A // Response Information 请求信息 UTF-8编码字符串 CONNACK ServerRef uint8 = 0x1C // Server Reference 服务端参考 UTF-8编码字符串 CONNACK, DISCONNECT ReasonString uint8 = 0x1F // Reason String 原因字符串 UTF-8编码字符串 CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK, DISCONNECT, AUTH ReceiveMaximum uint8 = 0x21 // Receive Maximum 接收最大数量 双字节整数 CONNECT, CONNACK TopicAM uint8 = 0x22 // Topic Alias Maximum 主题别名最大长度 双字节整数 CONNECT, CONNACK TopicAlias uint8 = 0x23 // Topic Alias 主题别名 双字节整数 PUBLISH MaximumQoS uint8 = 0x24 // Maximum QoS 最大QoS 字节 CONNACK RetainA uint8 = 0x25 //Retain Available 字节 CONNACK UserProperty uint8 = 0x26 //User Property 用户属性 UTF-8字符串对 CONNECT, CONNACK, PUBLISH, Will Properties, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, DISCONNECT, AUTH MaximumPS uint8 = 0x27 //Maximum Packet Size 最大报文长度 四字节整数 CONNECT, CONNACK WildcardSA uint8 = 0x28 // Wildcard Subscription Available 通配符订阅可用性 字节 CONNACK SubscriptionIA uint8 = 0x29 //Subscription Identifier Available 订阅标识符可用性 字节 CONNACK )
内置属性编号
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AUTHProtocol ¶
type AUTHProtocol struct { // 固定报头 // HeaderFlag uint8 = AUTH = 0xF0 // == 240 1111 0000 C<=>S // MsgLen *Fixed // 可变报头 AuthenticationReasonCode uint8 // PropertiesLength uint32 // 属性长度 // 属性内容 AuthenticationMethod string //认证方法 UTF-8编码字符串 AuthenticationData string // 认证数据 二进制数据 ReasonString string //原因字符串 UTF-8编码字符串 UserProperty map[string]string // 用户属性 字符串 }
AUTH = 0xF0 // == 240 1111 0000 C<=>S
func NewAUTHProtocol ¶
func NewAUTHProtocol() *AUTHProtocol
func NewAUTHProtocolF ¶
func NewAUTHProtocolF(f *Fixed) *AUTHProtocol
func (*AUTHProtocol) Pack ¶
func (s *AUTHProtocol) Pack() ([]byte, error)
func (*AUTHProtocol) UnPack ¶
func (s *AUTHProtocol) UnPack() error
type CONNACKProtocol ¶
type CONNACKProtocol struct { // 固定报头 // HeaderFlag uint8 = CONNACK = 0x20 第一个字节 // MsgLen = 2 对于 CONNACK 报文这个值等于 2 第二个字节 *Fixed // 可变报头 这个是特有的可变报头 ConnectAcknowledgeFlags uint8 // 第三个字节 必须是0 ConnectReturncode uint8 // 第四个字节 返回码 根据文档定义 PropertiesLength uint32 // 1-4 字节 和 msgLen 相同 SessionExpiryInterval uint32 // 会话过期间隔 四字节整数 ReceiveMaximum uint16 //接收最大数量 MaximumQoS uint8 // RetainAvailable uint8 // MaximumPacketSize uint32 //最大报文长度 AssignedClientIdentifier string //分配客户标识符 TopicAliasMaximum uint16 // 主题别名最大长度 ReasonString string // 原因字符串 UTF-8编码字符串 UserProperty map[string]string // 用户属性 WildcardSubscriptionAvailable uint8 // 通配符订阅可用性 字节 SubscriptionIdentifiersAvailable uint8 // 订阅标识符可用性 字节 ServerKeepAlive uint16 //服务端保活时间 双字节整数 ResponseInformation string // 请求信息 UTF-8编码字符串 ServerReference string // 服务端参考 UTF-8编码字符串 AuthenticationMethod string // 认证方法 AuthenticationData string // 认证数据 }
链接确认协议 服务端到客户端 CONNACK = 0x20
func NewCONNACKProtocol ¶
func NewCONNACKProtocol(code uint8) *CONNACKProtocol
func NewCONNACKProtocolF ¶
func NewCONNACKProtocolF(f *Fixed) *CONNACKProtocol
func (*CONNACKProtocol) Pack ¶
func (s *CONNACKProtocol) Pack() ([]byte, error)
func (s *CONNACKProtocol) Pack() ([]byte, error) {
// 2 打包数据 必须按照以下顺序写入 dataBuff := bytes.NewBuffer([]byte{}) // 2-1写入 消息类型 占据1个字节 if err := binary.Write(dataBuff, binary.LittleEndian, s.HeaderFlag); err != nil { return []byte{}, errors.New("SendCONNACK 写入HeaderFlag 错误" + err.Error()) } // 2-2在写入 消息长度 占据1个字节 必须转化长度 不然字节多了 if err := binary.Write(dataBuff, binary.LittleEndian, uint8(s.MsgLen)); err != nil { return []byte{}, errors.New("SendCONNACK 写入MsgLen 错误" + err.Error()) } // 2-3 写入 ConnectAcknowledgeFlags 占据1个字节 if err := binary.Write(dataBuff, binary.LittleEndian, s.ConnectAcknowledgeFlags); err != nil { return []byte{}, errors.New("SendCONNACK 写入ConnectAcknowledgeFlags 错误" + err.Error()) } // 2-4 写入 ConnectReturncode 一个字节 if err := binary.Write(dataBuff, binary.LittleEndian, s.ConnectReturncode); err != nil { return []byte{}, errors.New("SendCONNACK 写入ConnectReturncode 错误" + err.Error()) } return dataBuff.Bytes(), nil
}
func (*CONNACKProtocol) UnPack ¶
func (s *CONNACKProtocol) UnPack() error
type CONNECTProtocol ¶
type CONNECTProtocol struct { // 固定报头 *Fixed // 可变报头 ProtoNameLen uint16 // 两个字节 默认 4 ProtoName string // 根据ProtoNameLen 长度获取的数据 Version uint8 // 一个字节 协议级别 版本号 mqtt5.0 版本 0x05 ConnectFlag uint8 // 一个字节 链接标志 可以控制有效载荷 KeepAlive uint16 // 两个字节 大端解码返回数值 // 属性 内容 属性标识符后面跟随的就是属性内容 PropertiesLength uint32 // 1-4 字节 和 msgLen 相同 SessionExpiryInterval uint32 // 会话过期间隔 四字节整数 AuthenticationMethod string // 认证方法 //AuthenticationData []byte // 认证数据 AuthenticationData string // 认证数据 RequestProblemInformation uint8 //请求问题信息 RequestResponseInformation uint8 // 请求响应信息 ReceiveMaximum uint16 //接收最大数量 TopicAliasMaximum uint16 // 主题别名最大长度 UserProperty map[string]string // 用户属性 字符串对 [k1,v1][k2,v2] MaximumPacketSize uint32 //最大报文长度 ClientIDLength uint16 // 两个字节 大端解码返回数值 可以设定设定长度不要超过 200 ClientID string // 根据长度 获取数据 WillProperties uint32 // 遗嘱属性长度 PayloadFormatIndicator uint8 MessageExpiryInterval uint32 ContentType string ResponseTopic string //CorrelationData []byte CorrelationData string WillDelayInterval uint32 WillTopicLength uint16 // 大端解码返回数值 WillTopic string WillMessageLength uint16 WillMessage string UserNameLength uint16 UserName string PasswordLength uint16 Password string // 以下数据根据 ConnectFlag 解析出使用 CleanStart bool WillFlag bool WillRetain bool WillQos uint8 // AckCode 生成对应响应使用的AckCode AckCode uint8 }
链接协议结构体 客户端到服务端 CONNECT = 0x10
func NewCONNECTProtocol ¶
func NewCONNECTProtocol(f *Fixed) *CONNECTProtocol
func NewCONNECTProtocolClient ¶
func NewCONNECTProtocolClient(clientID, user, psd string) *CONNECTProtocol
func (*CONNECTProtocol) GetAckCode ¶
func (s *CONNECTProtocol) GetAckCode() uint8
func (*CONNECTProtocol) Pack ¶
func (s *CONNECTProtocol) Pack() ([]byte, error)
func (*CONNECTProtocol) UnPack ¶
func (s *CONNECTProtocol) UnPack() error
type DISCONNECTProtocol ¶
type DISCONNECTProtocol struct { // 固定报头 // HeaderFlag uint8 = DISCONNECT = 0xE0 // == 224 1110 0000 C=>S // MsgLen 变长字节 *Fixed // 断开原因 ReasonCode uint8 // 断开原因码 PropertiesLength uint32 // 1-4 字节 和 msgLen 相同 SessionExpiryInterval uint32 // 会话过期间隔 四字节整数 ReasonString string // 原因字符串 UTF-8编码字符串 UserProperty map[string]string // 用户属性 字符串 ServerReference string // 服务端参考 UTF-8编码字符串 }
客户端断开链接协议 DISCONNECT = 0xE0 // == 224 1110 0000 C=>S
func NewDISCONNECTProtocol ¶
func NewDISCONNECTProtocol(f *Fixed) *DISCONNECTProtocol
func (*DISCONNECTProtocol) UnPack ¶
func (s *DISCONNECTProtocol) UnPack() error
type Fixed ¶
type Fixed struct { // 固定报头 HeaderFlag uint8 // 协议类型 第1个字节 MsgLen uint32 // 剩余长度 1-4个字节 // 剩余字节 Data []byte }
固定报头 通用型 所有协议都有固定报头,都继承此结构体,同时实现了 接口
func (*Fixed) GetAckCode ¶
func (*Fixed) GetHeaderFlag ¶
type ImplMqttProto ¶
type PINGREQProtocol ¶
type PINGREQProtocol struct { // 固定报头 // HeaderFlag uint8 = PINGREQ = 0xC0 // == 192 1100 0000 C=>S // MsgLen = 0 必须为 0 *Fixed }
PINGREQ 心跳请求协议 PINGREQ = 0xC0 // == 192 1100 0000 C=>S
func NewPINGREQProtocol ¶
func NewPINGREQProtocol(f *Fixed) *PINGREQProtocol
type PINGRESPProtocol ¶
type PINGRESPProtocol struct { // 固定报头 // HeaderFlag uint8 = PINGRESP = 0xD0 // == 208 1101 0000 S=>C // MsgLen = 0 必须为 0 *Fixed }
PINGRESP 心跳响应协议 PINGRESP = 0xD0 // == 208 1101 0000 S=>C
func NewPINGRESPProtocol ¶
func NewPINGRESPProtocol() *PINGRESPProtocol
func (*PINGRESPProtocol) Pack ¶
func (s *PINGRESPProtocol) Pack() ([]byte, error)
type PUBACKProtocol ¶
type PUBACKProtocol struct { // 固定报头 // HeaderFlag uint8 = PUBACK = 0x40 // == 64 0100 0000 C<=>S // MsgLen = 2 *Fixed // 可变报头 // 等于需要确认的 PUBLISH 的 PacketIdentifier PacketIdentifier [2]byte // 两个字节 第一个标识 MSB 和 第二个标识LSB // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 ReasonCode uint8 // 使用响应码 PropertiesLength uint32 // 属性长度 }
PUBACK –发布确认 协议 PUBACK 报文是对 QoS 1 等级的 PUBLISH 报文的响应。 发送的 PUBLISH 报文必须包含报文标识符且 QoS 等于 1,DUP 等于 0 PUBACK = 0x40 // == 64 0100 0000 C<=>S
func NewPUBACKProtocol ¶
func NewPUBACKProtocol(pid [2]byte, rec uint8) *PUBACKProtocol
func NewPUBACKProtocolF ¶
func NewPUBACKProtocolF(f *Fixed) *PUBACKProtocol
func (*PUBACKProtocol) Pack ¶
func (s *PUBACKProtocol) Pack() ([]byte, error)
func (*PUBACKProtocol) UnPack ¶
func (s *PUBACKProtocol) UnPack() error
type PUBCOMPProtocol ¶
type PUBCOMPProtocol struct { // 固定报头 // HeaderFlag uint8 = PUBCOMP = 0x70 // ==112 0111 0000 C<=>S // MsgLen *Fixed // 可变报头 // 等于需要确认的 PUBLISH 的 PacketIdentifier PacketIdentifier [2]byte // 两个字节 // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 ReasonCode uint8 // 使用响应码 PropertiesLength uint32 // 属性长度 // AckCode 生成对应响应使用的AckCode AckCode uint8 }
发布完成协议 Qos2 最后响应 PUBCOMP = 0x70 // ==112 0111 0000 C<=>S
func NewPUBCOMPProtocol ¶
func NewPUBCOMPProtocol(pid [2]byte, rec uint8) *PUBCOMPProtocol
func NewPUBCOMPProtocolF ¶
func NewPUBCOMPProtocolF(f *Fixed) *PUBCOMPProtocol
func (*PUBCOMPProtocol) Pack ¶
func (s *PUBCOMPProtocol) Pack() ([]byte, error)
func (*PUBCOMPProtocol) UnPack ¶
func (s *PUBCOMPProtocol) UnPack() error
type PUBLISHProtocol ¶
type PUBLISHProtocol struct { // 固定报头 // HeaderFlag uint8 = PUBLISH = 0x30 // == 48 0011 xxxx 大于等于48 小于 64 C<=>S *Fixed // 可变报头 TopicNameLength uint16 // 两个字节 大端解码返回数值 TopicName string PacketIdentifier [2]byte // 两个字节 第一个标识 MSB 和 第二个标识LSB // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 // 属性 PropertiesLength uint32 // 1-4 字节 和 msgLen 相同 PayloadFormatIndicator uint8 // 载荷格式说明 字节 MessageExpiryInterval uint32 //消息过期时间 四字节整数 TopicAlias uint16 //主题别名 双字节整数 ResponseTopic string //响应主题 UTF-8编码字符串 CorrelationData string // 对比数据 UserProperty map[string]string // 用户属性 SubscriptionIdentifier uint32 // 定义标识符 变长字节整数 ContentType string // 有效载荷 剩余字节都是主题内容 Payload []byte // 解析Qos 用于处理 Qos uint8 // 解析 Retain 用于处理 Retain bool // 0 或者1 // AckCode 生成对应响应使用的AckCode AckCode uint8 }
发布协议 PUBLISH = 0x30 // == 48 0011 xxxx 大于等于48 小于 64 C<=>S
func NewPUBLISHProtocol ¶
func NewPUBLISHProtocol(f *Fixed) *PUBLISHProtocol
func (*PUBLISHProtocol) GetAckCode ¶
func (s *PUBLISHProtocol) GetAckCode() uint8
func (*PUBLISHProtocol) Pack ¶
func (s *PUBLISHProtocol) Pack() ([]byte, error)
func (*PUBLISHProtocol) UnPack ¶
func (s *PUBLISHProtocol) UnPack() error
type PUBRECProtocol ¶
type PUBRECProtocol struct { // 固定报头 // HeaderFlag uint8 = PUBREC = 0x50 // == 80 0101 0000 C<=>S // MsgLen = 2 *Fixed // 可变报头 // 等于需要确认的 PUBLISH 的 PacketIdentifier PacketIdentifier [2]byte // 两个字节 第一个标识 MSB 和 第二个标识LSB // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 ReasonCode uint8 // 使用响应码 PropertiesLength uint32 // 属性长度 }
发布收到 协议 Qos = 2 的publish 需要 这个响应 发送的 PUBLISH 报文必须包含报文标识符且 QoS 等于 2,DUP 等于 0 PUBREC = 0x50 // == 80 0101 0000 C<=>S
func NewPUBRECProtocol ¶
func NewPUBRECProtocol(pid [2]byte, rec uint8) *PUBRECProtocol
func NewPUBRECProtocolF ¶
func NewPUBRECProtocolF(f *Fixed) *PUBRECProtocol
func (*PUBRECProtocol) Pack ¶
func (s *PUBRECProtocol) Pack() ([]byte, error)
func (*PUBRECProtocol) UnPack ¶
func (s *PUBRECProtocol) UnPack() error
type PUBRELProtocol ¶
type PUBRELProtocol struct { // 固定报头 // HeaderFlag uint8 = PUBREL = 0x62 // ==98 0110 0010 C<=>S // MsgLen *Fixed // 可变报头 // 等于需要确认的 PUBLISH 的 PacketIdentifier PacketIdentifier [2]byte // 两个字节 // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 ReasonCode uint8 // 使用响应码 PropertiesLength uint32 // 属性长度 // AckCode 生成对应响应使用的AckCode AckCode uint8 }
发布释放 协议 Qos2 第二步 PUBREL = 0x62 // ==98 0110 0010 C<=>S
func NewPUBRELProtocol ¶
func NewPUBRELProtocol(f *Fixed) *PUBRELProtocol
func (*PUBRELProtocol) Pack ¶
func (s *PUBRELProtocol) Pack() ([]byte, error)
func (*PUBRELProtocol) UnPack ¶
func (s *PUBRELProtocol) UnPack() error
type SUBACKProtocol ¶
type SUBACKProtocol struct { // 固定报头 // HeaderFlag uint8 = SUBACK = 0x90 // ==144 1001 0000 S=>C // MsgLen = 1-4 *Fixed // 可变报头 PacketIdentifier [2]byte // 两个字节 和订阅相同 // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 // 属性 PropertiesLength uint32 // 1-4 字节 和 msgLen 相同 ReasonString string //原因字符串 UTF-8编码字符串 UserProperty map[string]string // 用户属性 字符串 // 有效载荷 使用响应码 从订阅处理后获取 ReturnCodeList []uint8 }
订阅确认 协议 SUBACK = 0x90 // ==144 1001 0000 S=>C PacketIdentifier 必须和SUBSCRIBE 相同
func NewSUBACKProtocol ¶
func NewSUBACKProtocol(tfl uint32, pid [2]byte, rcl []uint8) *SUBACKProtocol
func (*SUBACKProtocol) Pack ¶
func (s *SUBACKProtocol) Pack() ([]byte, error)
type SUBSCRIBEProtocol ¶
type SUBSCRIBEProtocol struct { // 固定报头 // HeaderFlag uint8 = SUBSCRIBE = 0x82 // == 130 1000 0010 C=>S // MsgLen = 1-4 , PacketIdentifier 两个字节 有效载荷多个 *Fixed // 可变报头 PacketIdentifier [2]byte // 两个字节 // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 // 属性 PropertiesLength uint32 // 1-4 字节 和 msgLen 相同 SubscriptionIdentifier uint32 // 变长字节整数 UserProperty map[string]string // 用户属性 字符串 // 有效载荷 TopicFilterList []*TopicFilter // AckCode 生成对应响应使用的AckCode AckCode uint8 }
func NewSUBSCRIBEProtocol ¶
func NewSUBSCRIBEProtocol(f *Fixed) *SUBSCRIBEProtocol
func (*SUBSCRIBEProtocol) GetAckCode ¶
func (s *SUBSCRIBEProtocol) GetAckCode() uint8
func (*SUBSCRIBEProtocol) UnPack ¶
func (s *SUBSCRIBEProtocol) UnPack() error
解包 订阅主题协议 1,创造协议 2,根据 固定报头 解包
type TopicFilter ¶
type TopicFilter struct { Identifier uint16 // 两个字节 FilterName string // 根据Identifier 长度获取 //QoS uint8 // 本组最后一个字节 只能 从常量 QoS0,1,2中取值 // todo 包含 Retain Handling 、RAP、NL、Qos Options uint8 // 本组最后一个字节 只能 从常量 QoS0,1,2中取值 }
SUBSCRIBEProtocol 使用
type UNSUBACKProtocol ¶
type UNSUBACKProtocol struct { // 固定报头 // HeaderFlag uint8 = UNSUBACK = 0xB0 // == 176 1011 0000 S=>C // MsgLen = 1-4 *Fixed // 可变报头 PacketIdentifier [2]byte // 两个字节 和取消订阅相同 // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 // 属性 PropertiesLength uint32 // 1-4 字节 和 msgLen 相同 ReasonString string //原因字符串 UTF-8编码字符串 UserProperty map[string]string // 用户属性 字符串 // 有效载荷 使用响应码 从订阅处理后获取 ReturnCodeList []uint8 }
取消订阅确认 协议 UNSUBACK = 0xB0 // == 176 1011 0000 S=>C
func NewUNSUBACKProtocol ¶
func NewUNSUBACKProtocol(tfl uint32, pid [2]byte, rcl []uint8) *UNSUBACKProtocol
func (*UNSUBACKProtocol) Pack ¶
func (s *UNSUBACKProtocol) Pack() ([]byte, error)
type UNSUBSCRIBEProtocol ¶
type UNSUBSCRIBEProtocol struct { // 固定报头 // HeaderFlag uint8 = UNSUBSCRIBE = 0xA2 // == 162 1010 0010 C=>S // MsgLen = 1-4 *Fixed // 可变报头 PacketIdentifier [2]byte // 两个字节 订阅确认还要使用相同的可变报头 // 报文标识符转化为 MsgId MsgId uint16 // PacketIdentifier = MsgID 大端存储 // 属性 PropertiesLength uint32 // 1-4 字节 和 msgLen 相同 UserProperty map[string]string // 用户属性 字符串 // 有效载荷 没有 QoS 所以至少是3个字节 TopicFilterList []*TopicFilter // AckCode 生成对应响应使用的AckCode AckCode uint8 }
取消订阅协议 UNSUBSCRIBE = 0xA2 // == 162 1010 0010 C=>S
func NewUNSUBSCRIBEProtocol ¶
func NewUNSUBSCRIBEProtocol(f *Fixed) *UNSUBSCRIBEProtocol
func (*UNSUBSCRIBEProtocol) GetAckCode ¶
func (s *UNSUBSCRIBEProtocol) GetAckCode() uint8
func (*UNSUBSCRIBEProtocol) UnPack ¶
func (s *UNSUBSCRIBEProtocol) UnPack() error
解包 订阅主题协议 1,创造协议 2,根据 固定报头 解包