Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client unsubscribe topic have problem #349

Closed
Scorpions777 opened this issue Dec 19, 2023 · 1 comment · Fixed by #353
Closed

Client unsubscribe topic have problem #349

Scorpions777 opened this issue Dec 19, 2023 · 1 comment · Fixed by #353
Labels
bug Something isn't working

Comments

@Scorpions777
Copy link

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
Copy link
Contributor

dadebue commented Dec 22, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants