mochi-mqtt / server

The fully compliant, embeddable high-performance Go MQTT v5 server for IoT, smarthome, and pubsub
MIT License
1.3k stars 223 forks source link

Client unsubscribe topic have problem #349

Closed Scorpions777 closed 10 months ago

Scorpions777 commented 11 months ago

I test in mqtt.js like below, unsubscribe thorw some exception, but it have no problem when i use emqx broker, mochi is v2.4.3 and v2.4.1

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
</head>

<body>
    <button onclick="subscribe()">订阅</button>
    <button onclick="publish()">发布</button>
    <button onclick="unsubscribe()">取消订阅</button>
</body>
<script>
    const clientId = 'mqttjs_' + Math.random().toString(16).substr(2, 8)

    const brokerUrl = 'ws://127.0.0.1:1882'
    // const brokerUrl = 'ws://broker.emqx.io:8083/mqtt'

    const options = {
        keepalive: 60,
        clientId: clientId,
        protocolId: 'MQTT',
        protocolVersion: 4,
        clean: true,
        reconnectPeriod: 1000,
        connectTimeout: 30 * 1000,
        will: {
            topic: 'WillMsg',
            payload: 'Connection Closed abnormally..!',
            qos: 0,
            retain: false
        },
    }

    const client = mqtt.connect(brokerUrl, options)

    client.on('error', (err) => {
        console.log('Connection error: ', err)
        client.end()
    })

    client.on('reconnect', () => {
        console.log('Reconnecting...')
    })

    client.on('connect', () => {
        console.log('Client connected:' + clientId)
    })

    client.on('message', (topic, message, packet) => {
        console.log('Received Message: ' + message.toString() + '\nOn topic: ' + topic)
    })

    function publish() {
        client.publish('testtopic', 'ws connection demo...!', { qos: 0, retain: false })
    }

    function unsubscribe() {
        client.unsubscribe('testtopic')
    }

    function subscribe() {
        client.subscribe('testtopic', { qos: 0 })
    }
</script>

</html>
dadebue commented 11 months ago

I just found exactly the same problem and was about to create an issue :)

Testing with https://github.com/mqttjs/MQTT.js and v2.4.4. The thrown error from MQTT.js is:

Error: Malformed unsuback, payload length must be 2