emqx / CocoaMQTT

MQTT 5.0 client library for iOS and macOS written in Swift
https://www.emqx.com/en
Other
1.57k stars 411 forks source link

Pos issue of FramePublish reading bytes #531

Open JyHu opened 1 year ago

JyHu commented 1 year ago

image

It seems that there is a missing byte in the offset calculation. In my actual testing, when sending a message to the backend using the publish method and receiving the data back, the received data cannot be parsed correctly. The following image shows the issue:

image

The data should have been a JSON object, but there is an extra byte in the received binary data, causing the conversion to fail. After changing the offset of 'pos' from 1 to 2, the received data can be parsed correctly.

The following is the publish code :

do {
    let topic = request.gateway
    let message = try CocoaMQTT5Message(topic: topic.rawValue, payload: request.params, qos: .qos1)
    let timestamp = UInt(delegate?.getCurrentTimestamp() ?? Date().timeIntervalSince1970 * 1000)
    let properties = MqttPublishProperties(
        responseTopic: topic.responseTopic,
        correlation: request.requestID,
        userProperty: [
            "accept": request.dataType.accept,
            "lang": request.language.rawValue,
            "timestamp": String(describing: timestamp),
            "resPrefix": resTopic
        ]
    )
    properties.contentType = "JSON"
    mqttClient.publish(message, properties: properties)
} catch {
    request.response?(.failure(.paramsError(error)))
}
leeway1208 commented 1 year ago

Hi. I don't think the bug is here. Because the fixed header is 1 byte. Does the demo have this issue? Or It cold be a bug from another location.😄

JyHu commented 1 year ago

Hi. I don't think the bug is here. Because the fixed header is 1 byte. Does the demo have this issue? Or It cold be a bug from another location.😄

I used the Eclipse Paho framework to integrate it into the project and conducted some testing. The results were fine. Upon comparing the binary data, I did notice that the pos value was missing one byte. However, I apologize that I don't have a deep understanding of the MQTT protocol, so I am unable to investigate the specific cause of the issue. 😢