Пример #1
0
 protected static MQTT mqttClient(String host, int port) {
   MQTT client = new MQTT();
   client.setCleanSession(true);
   client.setClientId("amc-" + Thread.currentThread().getId() + "-" + System.currentTimeMillis());
   client.setHost(URIs.newURI("tcp://" + host + ":" + port));
   return client;
 }
Пример #2
0
  private static FutureConnection createConnection(String host, String clientId) {

    try {
      MQTT client = new MQTT();
      client.setHost(host);
      client.setClientId(clientId);
      client.setCleanSession(false);
      return client.futureConnection();
    } catch (URISyntaxException e) {
      Log.e(logTag, e.getMessage());
      return null;
    }
  }
  private void connect() {

    if (((connection != null) && (!connection.isConnected())) || (connection == null)) {
      connection = mqtt.futureConnection();
      progressDialog = ProgressDialog.show(this, "", "Connecting...", true);
      Log.i(TAG, "connecting()");
      connection
          .connect()
          .then(
              onui(
                  new Callback<Void>() {
                    public void onSuccess(Void value) {
                      Log.i(TAG, "connected");
                      // connectButton.setEnabled(false);
                      progressDialog.dismiss();
                      toast("Connected");
                    }

                    public void onFailure(Throwable e) {
                      toast("Problem connecting to host");
                      Log.e(TAG, "Exception connecting to " + sAddress + " - " + e);
                      progressDialog.dismiss();
                    }
                  }));
    }
  }
  public void createConnection() {
    // Public Queue
    mqtt = new MQTT();
    if ("".equals(user)) user = userText.getText().toString().trim();

    Log.i(TAG, user + "request" + new java.util.Date().getTime());
    mqtt.setClientId(user + "request" + new java.util.Date().getTime());
    // sAddress = "tcp://www.zeroglitch.org:1883";
    sAddress = "tcp://192.168.1.101:1883";
    sAddress = "tcp://www.zeroglitch.org:" + "1883"; // + port;

    Log.i(TAG, "Creating connection to " + sAddress);

    try {
      mqtt.setHost(sAddress);
      Log.d(TAG, "Address set: " + mqtt.getHost());
      Log.d(TAG, "client set: " + mqtt.getClientId());
    } catch (URISyntaxException urise) {
      Log.e(TAG, "URISyntaxException connecting to " + sAddress + " - " + urise);
    }

    Log.i(TAG, "the user: " + user);
  }
Пример #5
0
 public AnotherMqttClient connect() throws IOException {
   connection = mqttClient.futureConnection();
   exec(connection.connect());
   new Thread() {
     @Override
     public void run() {
       while (true) {
         try {
           org.fusesource.mqtt.client.Message message = exec(connection.receive());
           messages.add(new Message(message.getTopic(), new String(message.getPayload())));
           message.ack();
         } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
         }
       }
     }
   }.start();
   exec(connection.subscribe(new Topic[] {new Topic("#", AT_LEAST_ONCE)}));
   return this;
 }
    @Override
    public void run() {
      try {
        MQTT mqtt = new MQTT();
        mqtt.setConnectAttemptsMax(1);
        mqtt.setReconnectAttemptsMax(0);
        mqtt.setKeepAlive((short) 300);
        Log.d(TAG, "Connecting to MQTT...");

        mqtt.setHost(this.hostname, this.port);
        BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        connection.disconnect();
        Log.d(TAG, "Connected to MQTT.");

        getActivity()
            .runOnUiThread(
                new Runnable() {

                  @Override
                  public void run() {
                    handleMqttSuccess();
                  }
                });
      } catch (final URISyntaxException e) {
        getActivity()
            .runOnUiThread(
                new Runnable() {

                  @Override
                  public void run() {
                    handleMqttError("MQTT broker hostname invalid.", e);
                  }
                });
      } catch (final Throwable t) {
        getActivity()
            .runOnUiThread(
                new Runnable() {

                  @Override
                  public void run() {
                    handleMqttError("Unable to connect to MQTT broker.", t);
                  }
                });
      }
    }
Пример #7
0
 private MQTT newClient(MqttLinkConfig config) {
   MQTT client = new MQTT();
   client.setClientId(config.getClientId());
   client.setHost(URIs.newURI("tcp://" + config.getHost() + ":" + config.getPort()));
   return client;
 }