hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
824 stars 153 forks source link

How to connect to websocket for the following setting? #613

Closed TomWangTW closed 5 months ago

TomWangTW commented 6 months ago

Checklist

❓ Question

How to connect to websocket for the following setting?

address: wss://xxx.com:8084

so i seperated the address to

  1. host: xxx.com
  2. port: 8084

I used below config but it always get error like this: cause: io.netty.handler.codec.http.websocketx.WebSocketHandshakeException: Invalid handshake response getStatus: 404 Not Found

but this address is work well with eclipse mqtt client

mqttClient = Mqtt3Client
                     .builder()
                     .serverHost(host) //xxx.com
                     .serverPort(port) //8084
                     .webSocketConfig()
                     .applyWebSocketConfig()
                     .identifier(mqttInfo.mqttId)
                     .simpleAuth()
                     .username(mqttInfo.mqttUsername)
                     .password(mqttInfo.mqttPassword.toByteArray())
                     .applySimpleAuth()
                     .buildAsync()
skidnight commented 5 months ago

@TomWangTW wss is secure web sockets and based on the configuration you posted the endpoint the client will try is not secure (ie. ws://xxx.com:8084). You should just have to add an ssl config to get around this error, like this:

        mClient = Mqtt3Client.builder()
                 .serverHost(host) //xxx.com
                 .serverPort(port) //8084
                 .sslWithDefaultConfig()
                 .webSocketConfig()
                 .applyWebSocketConfig()
                 .identifier(mqttInfo.mqttId)
                .simpleAuth()
                 .username(mqttInfo.mqttUsername)
                 .password(mqttInfo.mqttPassword.toByteArray())
                .applySimpleAuth()
                .buildAsync();

I would strongly suggest adding disconnect/connect listeners as well to get more information about the connection, and if possible consider using MQTT5 since it is more verbose with errors. Hope that helps!