當前位置: 首頁>>代碼示例>>Java>>正文


Java MqttConnectOptions.setKeepAliveInterval方法代碼示例

本文整理匯總了Java中org.eclipse.paho.client.mqttv3.MqttConnectOptions.setKeepAliveInterval方法的典型用法代碼示例。如果您正苦於以下問題:Java MqttConnectOptions.setKeepAliveInterval方法的具體用法?Java MqttConnectOptions.setKeepAliveInterval怎麽用?Java MqttConnectOptions.setKeepAliveInterval使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.eclipse.paho.client.mqttv3.MqttConnectOptions的用法示例。


在下文中一共展示了MqttConnectOptions.setKeepAliveInterval方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getMqttConnectOptions

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
public MqttConnectOptions getMqttConnectOptions() {
    MqttConnectOptions options = new MqttConnectOptions();
    options.setCleanSession(isCleanSession());
    options.setConnectionTimeout(getTimeout());
    options.setKeepAliveInterval(getKeepAlive());

    if (!getUsername().isEmpty()) {
        options.setUserName(getUsername());
    }

    if (!getPassword().isEmpty()) {
        options.setPassword(getPassword().toCharArray());
    }

    if (!getLwtTopic().isEmpty() && !getLwtPayload().isEmpty()) {
        options.setWill(getLwtTopic(), getLwtPayload().getBytes(), getLwtQos(), isLwtRetained());
    }

    return options;
}
 
開發者ID:emqtt,項目名稱:EMQ-Android-Toolkit,代碼行數:21,代碼來源:Connection.java

示例2: connectClient

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private void connectClient() {
  try {
    client = new MqttClient(broker, clientId);
    client.setCallback(this);
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName(user);
    connOpts.setPassword(password.toCharArray());
    connOpts.setCleanSession(true);
    connOpts.setKeepAliveInterval(OUTGOING_MQTT_KEEP_ALIVE);
    logger.debug("Connecting to broker:  " + broker);
    client.connect(connOpts);
    logger.debug("Connected");
  } catch (MqttException e) {
    logger.error("Failed to connect to MQTT client ( " + broker + "/" + clientId
        + ") for outbound messages");
    logger.error(e.getLocalizedMessage());
    e.printStackTrace();
  }
}
 
開發者ID:edgexfoundry,項目名稱:device-mqtt,代碼行數:20,代碼來源:OutgoingSender.java

示例3: startListening

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private void startListening() {
  logger.debug("Starting listening for response traffic");
  try {
    String url =
        cmdrespMqttBrokerProtocol + "://" + cmdrespMqttBroker + ":" + cmdrespMqttBrokerPort;
    client = new MqttClient(url, cmdrespMqttClientId);
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName(cmdrespMqttUser);
    connOpts.setPassword(cmdrespMqttPassword.toCharArray());
    connOpts.setCleanSession(true);
    connOpts.setKeepAliveInterval(cmdrespMqttKeepAlive);
    logger.debug("Connecting to response message broker:  " + cmdrespMqttBroker);
    client.connect(connOpts);
    logger.debug("Connected to response message broker");
    client.setCallback(this);
    client.subscribe(cmdrespMqttTopic, cmdrespMqttQos);
  } catch (MqttException e) {
    logger.error("Unable to connect to response message queue.  "
        + "Unable to respond to command requests.");
    e.printStackTrace();
    client = null;
  }
}
 
開發者ID:edgexfoundry,項目名稱:device-mqtt,代碼行數:24,代碼來源:CommandResponseListener.java

示例4: startListening

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private void startListening() {
  logger.debug("Starting listening for incoming traffic");
  try {
    String url =
        incomingMqttBrokerProtocol + "://" + incomingMqttBroker + ":" + incomingMqttBrokerPort;
    client = new MqttClient(url, incomingMqttClientId);
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName(incomingMqttUser);
    connOpts.setPassword(incomingMqttPassword.toCharArray());
    connOpts.setCleanSession(true);
    connOpts.setKeepAliveInterval(incomingMqttKeepAlive);
    logger.debug("Connecting to incoming message broker:  " + incomingMqttBroker);
    client.connect(connOpts);
    logger.debug("Connected to incoming message broker");
    client.setCallback(this);
    client.subscribe(incomingMqttTopic, incomingMqttQos);
  } catch (MqttException e) {
    logger.error("Unable to connect to incoming message queue.");
    e.printStackTrace();
    client = null;
  }
}
 
開發者ID:edgexfoundry,項目名稱:device-mqtt,代碼行數:23,代碼來源:IncomingListener.java

示例5: buildMqttConnectOptions

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private MqttConnectOptions buildMqttConnectOptions(AbstractAwsIotClient client, SocketFactory socketFactory) {
    MqttConnectOptions options = new MqttConnectOptions();

    options.setSocketFactory(socketFactory);
    options.setCleanSession(true);
    options.setConnectionTimeout(client.getConnectionTimeout() / 1000);
    options.setKeepAliveInterval(client.getKeepAliveInterval() / 1000);

    Set<String> serverUris = getServerUris();
    if (serverUris != null && !serverUris.isEmpty()) {
        String[] uriArray = new String[serverUris.size()];
        serverUris.toArray(uriArray);
        options.setServerURIs(uriArray);
    }

    if (client.getWillMessage() != null) {
        AWSIotMessage message = client.getWillMessage();

        options.setWill(message.getTopic(), message.getPayload(), message.getQos().getValue(), false);
    }

    return options;
}
 
開發者ID:aws,項目名稱:aws-iot-device-sdk-java,代碼行數:24,代碼來源:AwsIotMqttConnection.java

示例6: MyMqttCloudClient

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
public MyMqttCloudClient(String cloudBrokerAddress, String clientId) {
//		this._cloudTopic = cloudTopic;
		this._cloudBrokerAddress = cloudBrokerAddress;
		this._clientId = clientId;
		MemoryPersistence persistence = new MemoryPersistence();
		try {
			this._mqCloudClient = new MqttClient(this._cloudBrokerAddress,
					this._clientId, persistence);
			this._mqCloudClient.setCallback(this);
			MqttConnectOptions connOpts = new MqttConnectOptions();
//			connOpts.setCleanSession(true);
			connOpts.setConnectionTimeout(0);
			connOpts.setKeepAliveInterval(30);
			connOpts.setAutomaticReconnect(true);
			System.out.println("Connecting to cloud broker: " + this._cloudBrokerAddress);
			this._mqCloudClient.connect(connOpts);
			System.out.println("Connected");			
		} catch (MqttException me) {
			System.out.println("reason " + me.getReasonCode());
			System.out.println("msg " + me.getMessage());
			System.out.println("loc " + me.getLocalizedMessage());
			System.out.println("cause " + me.getCause());
			System.out.println("excep " + me);
			me.printStackTrace();
		}
	}
 
開發者ID:HPCC-Cloud-Computing,項目名稱:IoT,代碼行數:27,代碼來源:MyMqttCloudClient.java

示例7: CopyOfMyMqttCloudClient

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
public CopyOfMyMqttCloudClient(String cloudTopic, String cloudBrokerAddress, String clientId) {
		this._cloudTopic = cloudTopic;
		this._cloudBrokerAddress = cloudBrokerAddress;
		this._clientId = clientId;
		MemoryPersistence persistence = new MemoryPersistence();
		try {
			this._mqCloudClient = new MqttClient(this._cloudBrokerAddress,
					"sfdf", persistence);
			this._mqCloudClient.setCallback(this);
			MqttConnectOptions connOpts = new MqttConnectOptions();
//			connOpts.setCleanSession(true);
			connOpts.setKeepAliveInterval(30);
			System.out.println("Connecting to cloud broker: " + this._cloudBrokerAddress);
			this._mqCloudClient.connect(connOpts);
			System.out.println("Connected");			
		} catch (MqttException me) {
			System.out.println("reason " + me.getReasonCode());
			System.out.println("msg " + me.getMessage());
			System.out.println("loc " + me.getLocalizedMessage());
			System.out.println("cause " + me.getCause());
			System.out.println("excep " + me);
			me.printStackTrace();
		}
	}
 
開發者ID:HPCC-Cloud-Computing,項目名稱:IoT,代碼行數:25,代碼來源:CopyOfMyMqttCloudClient.java

示例8: connect

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
/**
 *
 *
 */
private void connect() throws MqttException {
    connOpt = new MqttConnectOptions();

    connOpt.setCleanSession(true);
    connOpt.setKeepAliveInterval(3600);
    connOpt.setConnectionTimeout(3600);
    connOpt.setUserName(sharedPref.getString("pref_username", ""));
    connOpt.setPassword(sharedPref.getString("pref_password", "").toCharArray());

    String tmpDir = createTempDir().getPath(); //System.getProperty("java.io.tmpdir");
    Log.i(TAG, "Persistence will be done in " + tmpDir);

    MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir);

    // Connect to Broker
    mClient = new MqttClient(sharedPref.getString("pref_url", "") + ":" + sharedPref.getString("pref_port", "1883"), android_id + "_client", dataStore);
    mClient.setCallback(this);
    mClient.connect(connOpt);
    Log.i(TAG, "Connected to " + sharedPref.getString("pref_url", ""));

}
 
開發者ID:caribewave,項目名稱:android-app,代碼行數:26,代碼來源:MQTTClient.java

示例9: optionsFromModel

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private MqttConnectOptions optionsFromModel(ConnectionModel model){

        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(model.isCleanSession());
        connOpts.setConnectionTimeout(model.getTimeout());
        connOpts.setKeepAliveInterval(model.getKeepAlive());
        if(!model.getUsername().equals(ActivityConstants.empty)){
            connOpts.setUserName(model.getUsername());
        }

        if(!model.getPassword().equals(ActivityConstants.empty)){
            connOpts.setPassword(model.getPassword().toCharArray());
        }
        /*
        if(!model.getLwtTopic().equals(ActivityConstants.empty) && !model.getLwtMessage().equals(ActivityConstants.empty)){
            connOpts.setWill(model.getLwtTopic(), model.getLwtMessage().getBytes(), model.getLwtQos(), model.isLwtRetain());
        }*/
        //   if(tlsConnection){
        //       // TODO Add Keys to conOpts here
        //       //connOpts.setSocketFactory();
        //   }
        return connOpts;
    }
 
開發者ID:Cirrus-Link,項目名稱:Sparkplug,代碼行數:24,代碼來源:MainActivity.java

示例10: run

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
public void run() {
	try {
		// Connect to the MQTT Server
		MqttConnectOptions options = new MqttConnectOptions();
		options.setAutomaticReconnect(true);
		options.setCleanSession(true);
		options.setConnectionTimeout(30);
		options.setKeepAliveInterval(30);
		options.setUserName(username);
		options.setPassword(password.toCharArray());
		client = new MqttClient(serverUrl, clientId);
		client.setTimeToWait(5000);						// short timeout on failure to connect
		client.connect(options);
		client.setCallback(this);
		
		// Just listen to all DDATA messages on spAv1.0 topics and wait for inbound messages
		client.subscribe("spBv1.0/#", 0);
	} catch(Exception e) {
		e.printStackTrace();
	}
}
 
開發者ID:Cirrus-Link,項目名稱:Sparkplug,代碼行數:22,代碼來源:SparkplugListener.java

示例11: startConnect

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private void startConnect(String clientID, String serverIP, String port) {
    //服務器地址
    String  uri ="tcp://";
    uri=uri+serverIP+":"+port;
    Log.d("MainActivity",uri+"  "+clientID);
    /**
     * 連接的選項
     */
    MqttConnectOptions conOpt = new MqttConnectOptions();
    /**設計連接超時時間*/
    conOpt.setConnectionTimeout(3000);
    /**設計心跳間隔時間300秒*/
    conOpt.setKeepAliveInterval(300);
    /**
     * 創建連接對象
     */
     client = new MqttAndroidClient(this,uri, clientID);
    /**
     * 連接後設計一個回調
     */
    client.setCallback(new MqttCallbackHandler(this, clientID));
    /**
     * 開始連接服務器,參數:ConnectionOptions,  IMqttActionListener
     */
    try {
        client.connect(conOpt, null, new ConnectCallBackHandler(this));
    } catch (MqttException e) {
        e.printStackTrace();
    }

}
 
開發者ID:LiuJunb,項目名稱:ActiveMQ-MQTT-Android,代碼行數:32,代碼來源:MainActivity.java

示例12: connectClient

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private void connectClient() throws Exception {
	try {
	    String mqttServerAddress = String.format("ssl://%s:%s", mqttBridgeHostname, mqttBridgePort);

		MqttConnectOptions connectOptions = new MqttConnectOptions();
	    connectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
	    connectOptions.setUserName(username);
	    password = createJwt(projectId, privateKeyFile, algorithm);
	    connectOptions.setPassword(password.toCharArray());

		connectOptions.setCleanSession(true);
		connectOptions.setKeepAliveInterval(keepAlive);

	    client = new MqttClient(mqttServerAddress, clientId, new MemoryPersistence());
		client.setCallback(this);

		logger.debug("Connecting to broker:  " + mqttServerAddress);
		client.connect(connectOptions);
		logger.debug("Connected");
		
	} catch (Exception e) {
		logger.error("Failed to connect to MQTT client ( " + mqttBridgeHostname + ":" + mqttBridgePort + "/" + clientId + ") for outbound messages");
		throw e;
	}
}
 
開發者ID:edgexfoundry,項目名稱:export-distro,代碼行數:26,代碼來源:IotCoreMQTTSender.java

示例13: pubMsg

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
public static void pubMsg(String tcpUrl, String clientId, String topicName,
			String message) throws MqttException, UnsupportedEncodingException {
		MqttClient client = new MqttClient(tcpUrl, clientId);
		MqttConnectOptions mqcConf = new MqttConnectOptions();
		mqcConf.setConnectionTimeout(300);
		mqcConf.setKeepAliveInterval(1200);
		client.connect(mqcConf);

		MqttTopic topic = client.getTopic(topicName);
		topic.publish(message.getBytes("utf8"), 1, false);

//		client.close();
	}
 
開發者ID:osswangxining,項目名稱:mqttserver,代碼行數:14,代碼來源:PubWebMessage.java

示例14: pubMsg

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
public static void pubMsg(String tcpUrl, String clientId, String topicName)
    throws MqttException, UnsupportedEncodingException {
  MqttClient client = new MqttClient(tcpUrl, clientId);
  MqttConnectOptions mqcConf = new MqttConnectOptions();
  mqcConf.setConnectionTimeout(300);
  mqcConf.setKeepAliveInterval(1200);
  client.connect(mqcConf);

  MqttTopic topic = client.getTopic(topicName);
  for (int i = 0; i < 10; i++) {
    String message = "{\"id\":" + (i+1) + ",\"temp\":12}";
    topic.publish(message.getBytes("utf8"), 1, false);
  }
  client.disconnect();
}
 
開發者ID:osswangxining,項目名稱:mqttserver,代碼行數:16,代碼來源:PubMessage.java

示例15: CopyOfMyMqttFogClient

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
public CopyOfMyMqttFogClient(String fogTopic, String fogBrokerAddress, String cloudTopic, String cloudBrokerAddress, String fogClientId, String cloudClientId) {
		this._fogTopic = fogTopic;
		this._cloudTopic = cloudTopic;
		this._fogBrokerAddress = fogBrokerAddress;
		this._cloudBrokerAddress = cloudBrokerAddress;
		this._fogClientId = fogClientId;
		this._cloudClientId = cloudClientId;
		MemoryPersistence persistence = new MemoryPersistence();
//		try {
//			this._mqCloudClient = new MqttClient(this._cloudBrokerAddress,
//					_cloudClientId, persistence);
//			this._mqCloudClient.setCallback(this);
//			MqttConnectOptions connOpts = new MqttConnectOptions();
//			connOpts.setCleanSession(true);
//			connOpts.setKeepAliveInterval(30);
//			System.out.println("Connecting to cloud broker: " + this._cloudBrokerAddress);
//			this._mqCloudClient.connect(connOpts);
//			System.out.println("Connected");			
//		} catch (MqttException me) {
//			System.out.println("reason " + me.getReasonCode());
//			System.out.println("msg " + me.getMessage());
//			System.out.println("loc " + me.getLocalizedMessage());
//			System.out.println("cause " + me.getCause());
//			System.out.println("excep " + me);
//			me.printStackTrace();
//		}		
		try {
			this._mqFogClient = new MqttClient(this._fogBrokerAddress, this._fogClientId,
					persistence);
			MqttConnectOptions connOpts = new MqttConnectOptions();
			connOpts.setCleanSession(true);
			connOpts.setKeepAliveInterval(30);
			System.out.println("Connecting to fog broker: " + this._fogBrokerAddress);
			this._mqFogClient.connect(connOpts);
			System.out.println("Connected");
		} catch (MqttException e) {
			e.printStackTrace();
		}
//		this._mqCloudClient = new MyMqttCloudClient(this._cloudTopic, this._cloudBrokerAddress, "oneM2M publisher");
	}
 
開發者ID:HPCC-Cloud-Computing,項目名稱:IoT,代碼行數:41,代碼來源:CopyOfMyMqttFogClient.java


注:本文中的org.eclipse.paho.client.mqttv3.MqttConnectOptions.setKeepAliveInterval方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。