0

I have a mqtt client connecting to a broker. My broker intercept connection to get token from it and do some works. I want to send a token as query param when connecting.

my client connect like this:

        MqttAsyncClient sampleClient = new MqttAsyncClient(broker, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();

        connOpts.setCleanSession(false);

        connOpts.setAutomaticReconnect(true);
        connOpts.setKeepAliveInterval(MqttConnectOptions.KEEP_ALIVE_INTERVAL_DEFAULT);

        connOpts.setConnectionTimeout(MqttConnectOptions.CONNECTION_TIMEOUT_DEFAULT); 
        IMqttToken token = sampleClient.connect(connOpts);

How can I do this? Could someone help me ?

Aymen Ragoubi
  • 298
  • 5
  • 22

1 Answers1

0

This might be possible if you modify the method sendHandshakeRequest in the class WebSocketHandshake:

private void sendHandshakeRequest(String key) throws IOException{

        pw.print("Upgrade: websocket" + LINE_SEPARATOR);
        pw.print("Connection: Upgrade" + LINE_SEPARATOR);
        pw.print("Sec-WebSocket-Key: " + key + LINE_SEPARATOR);
        pw.print("Sec-WebSocket-Protocol: mqttv3.1" + LINE_SEPARATOR);
        pw.print("Sec-WebSocket-Version: 13" + LINE_SEPARATOR);

        // TODO add the header with your token here

Since the method is private and the class is in internal package, you have no other choice, but compile your own custom version of the Paho library.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416