问题描述
我正在尝试使用Eclipse Paho创建Java Mqtt客户端,该客户端可以使用通过AwsIotWebSocketUrlSigner
的{{1}}方法生成的sigv4预签名URL连接到AWS IoT Messagebroker。该连接将通过Websockets使用MQTT,并且URL语法以“ wss://”开头。
连接代码如下所示。
getSignedUrl
我一直低于异常。上面的connect()失败了。
IMqttAsyncclient client = new MqttAsyncclient(*presignedUrl*,MqttAsyncclient.generateClientId(),new MemoryPersistence());
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
IMqttToken token = client.connect(options);
token.waitForCompletion();
client.setCallback( *callBackObject* );
client.subscribe(topic,AWSIotQos.QOS1.getValue());
在Eclipse Paho代码中,验证WSS握手时会在此处发生异常。
MqttException (0) - java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)
at java.lang.Thread.run(Thread.java:749)
Caused by: java.lang.NullPointerException
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.receiveHandshakeResponse(WebSocketHandshake.java:133)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketHandshake.execute(WebSocketHandshake.java:74)
at org.eclipse.paho.client.mqttv3.internal.websocket.WebSocketSecureNetworkModule.start(WebSocketSecureNetworkModule.java:77)
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)
我能够使用Javascript客户端和presignedUrl进行连接。
非常感谢您提供帮助/示例代码。
解决方法
在查看AWS Sdk如何使用底层Eclipse Paho时发现了这一点。 https://github.com/aws/aws-iot-device-sdk-java/blob/647449e654096172ebfcc31d79a8c582f952219d/aws-iot-device-sdk-java/src/main/java/com/amazonaws/services/iot/client/core/AwsIotWebsocketConnection.java#L46
正在将端口号443添加到clientEndpoint。显然我没有的presignedUrl。因此,我将signingUrl更改为也具有端口号,并且该端口有效。