コード例 #1
0
  /**
   * Publish / send a message to an MQTT server
   *
   * @param topicName the name of the topic to publish to
   * @param qos the quality of service to delivery the message at (0,1,2)
   * @param payload the set of bytes to send to the MQTT server
   * @throws MqttException
   */
  public void publish(String topicName, int qos, byte[] payload) throws MqttException {

    // Connect to the MQTT server
    // issue a non-blocking connect and then use the token to wait until the
    // connect completes. An exception is thrown if connect fails.
    log("Connecting to " + brokerUrl + " with client ID " + client.getClientId());
    IMqttToken conToken = client.connect(conOpt, null, null);
    conToken.waitForCompletion();
    log("Connected");

    String time = new Timestamp(System.currentTimeMillis()).toString();
    log("Publishing at: " + time + " to topic \"" + topicName + "\" qos " + qos);

    // Construct the message to send
    MqttMessage message = new MqttMessage(payload);
    message.setQos(qos);

    // Send the message to the server, control is returned as soon
    // as the MQTT client has accepted to deliver the message.
    // Use the delivery token to wait until the message has been
    // delivered
    IMqttDeliveryToken pubToken = client.publish(topicName, message, null, null);
    pubToken.waitForCompletion();
    log("Published");

    // Disconnect the client
    // Issue the disconnect and then use a token to wait until
    // the disconnect completes.
    log("Disconnecting");
    IMqttToken discToken = client.disconnect(null, null);
    discToken.waitForCompletion();
    log("Disconnected");
  }
コード例 #2
-1
  /**
   * Subscribe to a topic on an MQTT server Once subscribed this method waits for the messages to
   * arrive from the server that match the subscription. It continues listening for messages until
   * the enter key is pressed.
   *
   * @param topicName to subscribe to (can be wild carded)
   * @param qos the maximum quality of service to receive messages at for this subscription
   * @throws MqttException
   */
  public void subscribe(String topicName, int qos) throws MqttException {

    // Connect to the MQTT server
    // issue a non-blocking connect and then use the token to wait until the
    // connect completes. An exception is thrown if connect fails.
    log("Connecting to " + brokerUrl + " with client ID " + client.getClientId());
    IMqttToken conToken = client.connect(conOpt, null, null);
    conToken.waitForCompletion();
    log("Connected");

    // Subscribe to the requested topic.
    // Control is returned as soon client has accepted to deliver the subscription.
    // Use a token to wait until the subscription is in place.
    log("Subscribing to topic \"" + topicName + "\" qos " + qos);

    IMqttToken subToken = client.subscribe(topicName, qos, null, null);
    subToken.waitForCompletion();
    log("Subscribed to topic \"" + topicName);

    // Continue waiting for messages until the Enter is pressed
    log("Press <Enter> to exit");
    try {
      System.in.read();
    } catch (IOException e) {
      // If we can't read we'll just exit
    }

    // Disconnect the client
    // Issue the disconnect and then use the token to wait until
    // the disconnect completes.
    log("Disconnecting");
    IMqttToken discToken = client.disconnect(null, null);
    discToken.waitForCompletion();
    log("Disconnected");
  }