paho.mqtt.java icon indicating copy to clipboard operation
paho.mqtt.java copied to clipboard

Getting Exception Caused by: MqttException (0) - java.io.IOException: WebSocket Response header: Incorrect upgrade.

Open harialgat opened this issue 2 years ago • 1 comments

Please fill out the form below before submitting, thank you!

  • [x] Bug exists Release Version 1.2.5 ( Master Branch)

While connecting to mqtt broker getting this Exception.

Version : 1.2.5

Gradle dependency : implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'

code being used to connect

  private MqttAsyncClient instantiateNewMqttConnection()  {
        logger.info("Intantiate new MQTT connection....");
        logger.info("Connecting to MQTT Broker: " + this.params.url);
        MqttAsyncClient client = null;
        try {
            client = new MqttAsyncClient(this.params.url, this.params.clientId, new MemoryPersistence());
            client.setCallback(new MqttConnectionCallBack(this.params.clientId));
            MqttConnectOptions options = new MqttConnectOptions();
           // options.setSkipPortDuringHandshake(true);        
            options.setCleanSession(true);
            client.connect(options).waitForCompletion();
        } catch (MqttException e) {
            logger.info("MQTT Connection Failed");
            logger.info(e.getMessage());
            logger.info(e.getCause().toString());
            logger.info(e.getStackTrace().toString());
            Assert.fail("Failed due to MQTT connection could not be made");
        }
        return client;
    }

Same result with below code

    private MqttClient instantiateNewMqttConnection()  {
        logger.info("Intantiate new MQTT connection....");
        logger.info("Connecting to MQTT Broker: " + this.params.url);
        MqttClient client = null;
        try {
            client = new MqttClient(this.params.url, this.params.clientId, new MemoryPersistence());
            client.setCallback(new MqttConnectionCallBack(this.params.clientId));
            MqttConnectOptions options = new MqttConnectOptions();
           // options.setSkipPortDuringHandshake(true);
            options.setCleanSession(true);
            client.connect(options);
        } catch (MqttException e) {
            logger.info("MQTT Connection Failed");
            logger.info(e.getMessage());
            logger.info(e.getCause().toString());
            logger.info(e.getStackTrace().toString());
            Assert.fail("Failed due to MQTT connection could not be made");
        }
        return client;
    }

Note: URL starts with wss:// and it is being used to connect with AWSIOTMQTT broker.

harialgat avatar Apr 05 '23 11:04 harialgat

You don't show the URL, but I'm guessing your URL has a slash at the end. If you don't have any path, the client automatically adds "/mqtt", that is the default path according to the spec. I run into a similar when testing against Azure's Event Grid and it really doesn't show anywhere that they require an "/mqtt" path.

kaosko avatar Apr 06 '25 05:04 kaosko