我正在使用 mqtt_mutual_auth demo 订阅主题并收听传入消息。我使用 C SDK 进行订阅,使用 python SDK 发布消息。
订阅者代码
int main( int argc,
char ** argv )
{
int returnStatus = EXIT_SUCCESS;
MQTTContext_t mqttContext = { 0 };
NetworkContext_t networkContext = { 0 };
OpensslParams_t opensslParams = { 0 };
bool clientSessionPresent = false;
struct timespec tp;
( void ) argc;
( void ) argv;
/* Set the pParams member of the network context with desired transport. */
networkContext.pParams = &opensslParams;
/* Seed pseudo random number generator (provided by ISO C standard library) for
* use by retry utils library when retrying failed network operations. */
/* Get current time to seed pseudo random number generator. */
( void ) clock_gettime( CLOCK_REALTIME, &tp );
/* Seed pseudo random number generator with nanoseconds. */
srand( tp.tv_nsec );
/* Initialize MQTT library. Initialization of the MQTT library needs to be
* done only once in this demo. */
returnStatus = initializeMqtt( &mqttContext, &networkContext );
if( returnStatus == EXIT_SUCCESS )
{
/* Attempt to connect to the MQTT broker. If connection fails, retry after
* a timeout. Timeout value will be exponentially increased till the maximum
* attempts are reached or maximum timeout value is reached. The function
* returns EXIT_FAILURE if the TCP connection cannot be established to
* broker after configured number of attempts. */
returnStatus = connectToServerWithBackoffRetries( &networkContext );
if( returnStatus == EXIT_FAILURE )
{
/* Log error to indicate connection failure after all
* reconnect attempts are over. */
LogError( ( "Failed to connect to MQTT broker %.*s.",
AWS_IOT_ENDPOINT_LENGTH,
AWS_IOT_ENDPOINT ) );
}
else
{
/* If TLS session is established, execute Subscribe/Publish loop. */
returnStatus = subscribePublishLoop( &mqttContext, &clientSessionPresent );
}
/* End TLS session, then close TCP connection. */
}
printf("Now wait for incoming publish \n");
while(1)
{
sleep(1);
}
( void ) Openssl_Disconnect( &networkContext );
return returnStatus;
}
发布者代码
if __name__ == '__main__':
# Spin up resources
event_loop_group = io.EventLoopGroup(1)
host_resolver = io.DefaultHostResolver(event_loop_group)
client_bootstrap = io.ClientBootstrap(event_loop_group, host_resolver)
mqttendpoint = "aqwfuwdaruggr-ats.iot.us-east-1.amazonaws.com"
mqtt_connection = mqtt_connection_builder.mtls_from_path(
endpoint = mqttendpoint,
cert_filepath= "/home/mukesh/aws_mqtt/6fce06e56a-certificate.pem.crt",
pri_key_filepath="/home/mukesh/aws_mqtt/6fce06e56a-private.pem.key",
client_bootstrap=client_bootstrap,
ca_filepath="/home/mukesh/aws_mqtt/AmazonRootCA1.crt",
on_connection_interrupted=on_connection_interrupted,
on_connection_resumed=on_connection_resumed,
client_id=args.client_id,
clean_session=False,
keep_alive_secs=6)
print("Connecting to {} with client ID '{}'...".format(
mqttendpoint, args.client_id))
connect_future = mqtt_connection.connect()
# Future.result() waits until a result is available
connect_future.result()
print("Connected!")
# Publish message to server desired number of times.
# This step is skipped if message is blank.
# This step loops forever if count was set to 0.
if args.message:
if args.count == 0:
print ("Sending messages until program killed")
else:
print ("Sending {} message(s)".format(args.count))
publish_topic = "device1/server/live"
publish_count = 1
while (publish_count <= args.count) or (args.count == 0):
message = "{} [{}]".format(args.message, publish_count)
print("Publishing message to topic '{}': {}".format(publish_topic, message))
mqtt_connection.publish(
topic=publish_topic,
payload=message,
qos=mqtt.QoS.AT_LEAST_ONCE)
time.sleep(1)
publish_count += 1
# Disconnect
print("Disconnecting...")
disconnect_future = mqtt_connection.disconnect()
disconnect_future.result()
print("Disconnected!")
输出: 订阅者首先启动。 Publisher 在 10 秒后运行。
任何人都可以请建议我出了什么问题。我已将选项设置为持久会话。所以我的理解是,aws iot 核心代理应该有一份未确认的 QOS1 消息的副本并将其发送给订阅者。