Exemplo n.º 1
0
elif io.is_alpn_available():
    port = 443
    if tls_options:
        tls_options.alpn_list = ['x-amzn-mqtt-ca']
else:
    port = 8883

tls_context = io.ClientTlsContext(tls_options) if tls_options else None
mqtt_client = mqtt.Client(client_bootstrap, tls_context)

# Connect
print("Connecting to {}:{} with client-id:{}".format(args.endpoint, port, CLIENT_ID))
mqtt_connection = mqtt.Connection(
    client=mqtt_client,
    host_name=args.endpoint,
    port=port,
    client_id=CLIENT_ID,
    on_connection_interrupted=on_connection_interrupted,
    on_connection_resumed=on_connection_resumed)

connect_results = mqtt_connection.connect().result(TIMEOUT)
assert(connect_results['session_present'] == False)

# Subscribe
print("Subscribing to:", TOPIC)
qos = mqtt.QoS.AT_LEAST_ONCE
subscribe_future, subscribe_packet_id = mqtt_connection.subscribe(
    topic=TOPIC,
    qos=qos,
    callback=on_receive_message)
subscribe_results = subscribe_future.result(TIMEOUT)
Exemplo n.º 2
0
    port = args.port
elif io.is_alpn_available():
    port = 443
    if tls_options:
        tls_options.alpn_list = 'x-amzn-mqtt-ca'
else:
    port = 8883

tls_context = io.ClientTlsContext(tls_options) if tls_options else None
mqtt_client = mqtt.Client(client_bootstrap, tls_context)

# Connect
print("Connecting to {}:{} with client-id:{}".format(args.endpoint, port,
                                                     CLIENT_ID))
mqtt_connection = mqtt.Connection(
    client=mqtt_client,
    on_connection_interrupted=on_connection_interrupted,
    on_connection_resumed=on_connection_resumed)

connect_results = mqtt_connection.connect(client_id=CLIENT_ID,
                                          host_name=args.endpoint,
                                          port=port).result(TIMEOUT)
assert (connect_results['session_present'] == False)

# Subscribe
print("Subscribing to:", TOPIC)
qos = mqtt.QoS.AT_LEAST_ONCE
subscribe_future, subscribe_packet_id = mqtt_connection.subscribe(
    topic=TOPIC, qos=qos, callback=on_receive_message)
subscribe_results = subscribe_future.result(TIMEOUT)
assert (subscribe_results['packet_id'] == subscribe_packet_id)
assert (subscribe_results['topic'] == TOPIC)
Exemplo n.º 3
0
    if args.root_ca:
        tls_options.override_default_trust_store_from_path(ca_dirpath=None,
            ca_filepath=args.root_ca)
    tls_ctx = io.ClientTlsContext(options=tls_options)
    client = mqtt.Client(client_bootstrap, tls_ctx)

    username = args.username
    if args.authorizer_name:
        username += f'?x-amz-customauthorizer-name={args.authorizer_name}'
    if args.token:
        username += f'&{args.token_name}={args.token}'
    mqtt_connection = mqtt.Connection(client=client,
        host_name=args.endpoint,
        port=443,
        on_connection_interrupted=on_connection_interrupted,
        on_connection_resumed=on_connection_resumed,
        client_id=args.client_id,
        clean_session=True,
        keep_alive_secs=6,
        username=username,
        password=args.password)

    print("Connecting to {} with client ID '{}'...".format(
        args.endpoint, args.client_id))

    connect_future = mqtt_connection.connect()

    # Future.result() waits until a result is available
    connect_future.result()
    print("Connected!")

    # Subscribe
Exemplo n.º 4
0
    socket_options = io.SocketOptions()

    if args.root_ca:
        tls_options.override_default_trust_store_from_path(
            ca_dirpath=None, ca_filepath=args.root_ca)
    tls_ctx = io.ClientTlsContext(options=tls_options)

    client = mqtt.Client(client_bootstrap, tls_ctx)

    mqtt_connection = mqtt.Connection(
        client=client,
        host_name=args.endpoint,
        port=443,
        on_connection_interrupted=on_connection_interrupted,
        on_connection_resumed=on_connection_resumed,
        client_id=args.id,
        clean_session=True,
        keep_alive_secs=6,
        socket_options=socket_options,
        use_websockets=True,
        websocket_handshake_transform=add_headers)

    print("Connecting to {} with client ID '{}'...".format(
        args.endpoint, args.id))

    connect_future = mqtt_connection.connect()

    # Future.result() waits until a result is available
    connect_future.result()
    print("Connected!")