mqttjs / MQTT.js

The MQTT client for Node.js and the browser
Other
8.55k stars 1.41k forks source link

[Bug]: url path is removed at wechat environment #1892

Closed timzaak closed 3 months ago

timzaak commented 3 months ago

MQTTjs Version

5.7.0

Broker

NO

Environment

Browser

Description

// use string
ws.connect('wxs://www.example.com/mqtt', {timerVariant: 'native',})
// use object
ws.connect({
    host: 'www.example.com',
    path: '/mqtt',
    protocol: 'wxs',
    timerVariant: 'native',
})

the url would change to wss://www.example.com/

【expect】 the url would be wss://www.example.com/mqtt

Minimal Reproduction

as description

Debug logs

NO LOG

timzaak commented 3 months ago

https://github.com/mqttjs/MQTT.js/blob/82a4b790ab11433651a961b9eff8826d4b29b2bd/src/lib/connect/index.ts#L113

This delete, need add

    if (opts.unixSocket) {
        opts.protocol = opts.protocol.replace('+unix', '') as MqttProtocol
                 // the next line needs change to this
    } else if (!opts.protocol?.startsWith('ws') && !opts.protocol?.startsWith('wx') && !opts.protocol?.startsWith('ali')) {
        // consider path only with ws protocol or unix socket
        // url.parse could return path (for example when url ends with a `/`)
        // that could break the connection. See https://github.com/mqttjs/MQTT.js/pull/1874
        delete opts.path
    }
robertsLando commented 3 months ago

!opts.protocol?.startsWith('wx')

This should be true if you are using protocol wxs so the path should not be deleted from options