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


Java MqttConnectOptions.setMqttVersion方法代碼示例

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


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

示例1: initializeMqttClient

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private void initializeMqttClient()
    throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {

    mqttClient = new MqttClient(cloudIotOptions.getBrokerUrl(),
        cloudIotOptions.getClientId(), new MemoryPersistence());

    MqttConnectOptions options = new MqttConnectOptions();
    // Note that the the Google Cloud IoT only supports MQTT 3.1.1, and Paho requires that we
    // explicitly set this. If you don't set MQTT version, the server will immediately close its
    // connection to your device.
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
    options.setUserName(CloudIotOptions.UNUSED_ACCOUNT_NAME);

    // generate the jwt password
    options.setPassword(mqttAuth.createJwt(cloudIotOptions.getProjectId()));

    mqttClient.connect(options);
    mReady.set(true);
}
 
開發者ID:jpuderer,項目名稱:Taxi-Datalogger,代碼行數:20,代碼來源:MQTTPublisher.java

示例2: refusedUnacceptableProtocolVersion

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
@Test
public void refusedUnacceptableProtocolVersion(TestContext context) {

  this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION;

  try {
    MemoryPersistence persistence = new MemoryPersistence();
    MqttConnectOptions options = new MqttConnectOptions();
    // trying the old 3.1
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
    MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
    client.connect(options);
    context.fail();
  } catch (MqttException e) {
    context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_INVALID_PROTOCOL_VERSION);
  }
}
 
開發者ID:vert-x3,項目名稱:vertx-mqtt,代碼行數:18,代碼來源:MqttServerConnectionTest.java

示例3: refusedClientIdZeroBytes

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
@Test
public void refusedClientIdZeroBytes(TestContext context) {

  this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;

  try {
    MemoryPersistence persistence = new MemoryPersistence();
    MqttConnectOptions options = new MqttConnectOptions();
    options.setCleanSession(false);
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
    MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "", persistence);
    client.connect(options);
    context.fail();
  } catch (MqttException e) {
    context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_INVALID_CLIENT_ID);
    context.assertNotNull(rejection);
  }
}
 
開發者ID:vert-x3,項目名稱:vertx-mqtt,代碼行數:19,代碼來源:MqttServerConnectionTest.java

示例4: testInvalidClientIdentifier

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
@Test
public void testInvalidClientIdentifier(TestContext context) throws Exception {

  MemoryPersistence persistence = new MemoryPersistence();
  MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "invalid-id-with-24-chars", persistence);
  MqttConnectOptions options = new MqttConnectOptions();
  options.setMqttVersion(MQTT_VERSION_3_1);

  try {

    client.connect(options);
    context.assertTrue(false);

  } catch (MqttException ignore) {
    context.assertTrue(true);
  }
}
 
開發者ID:vert-x3,項目名稱:vertx-mqtt,代碼行數:18,代碼來源:MqttServerClientIdentifierTest.java

示例5: testValidClientIdentifier

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
@Test
public void testValidClientIdentifier(TestContext context) throws Exception {

  MemoryPersistence persistence = new MemoryPersistence();
  MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "id-madeof-23-characters", persistence);
  MqttConnectOptions options = new MqttConnectOptions();
  options.setMqttVersion(MQTT_VERSION_3_1);

  try {

    client.connect(options);
    context.assertTrue(true);

  } catch (MqttException ignore) {
    context.assertTrue(false);
  }
}
 
開發者ID:vert-x3,項目名稱:vertx-mqtt,代碼行數:18,代碼來源:MqttServerClientIdentifierTest.java

示例6: 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

示例7: doDemo

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
public void doDemo(String tcpUrl, String clientId, String topicName) {
	try {
	  MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
	  mqttConnectOptions.setMqttVersion(4);
		client = new MqttClient(tcpUrl, clientId);
		client.connect(mqttConnectOptions);
		client.setCallback(this);
		client.subscribe(topicName);
	} catch (MqttException e) {
		e.printStackTrace();
	}
}
 
開發者ID:osswangxining,項目名稱:mqttserver,代碼行數:13,代碼來源:SubscribeMessage.java

示例8: initializeMqttClient

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private void initializeMqttClient()
    throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {

    mqttClient = new MqttClient(mMqttOptions.getBrokerUrl(),
            mMqttOptions.getClientId(), new MemoryPersistence());

    MqttConnectOptions options = new MqttConnectOptions();
    // Note that the the Google Cloud IoT only supports MQTT 3.1.1, and Paho requires that we
    // explicitly set this. If you don't set MQTT version, the server will immediately close its
    // connection to your device.
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
    options.setUserName(CloudIotCoreOptions.UNUSED_ACCOUNT_NAME);
    options.setAutomaticReconnect(true);

    // generate the jwt password
    options.setPassword(mqttAuth.createJwt(mMqttOptions.getProjectId()));

    mqttClient.setCallback(this);
    mqttClient.connect(options);

    if(mqttClient.isConnected()) {
        try{
            mSubTopic = "/devices/sense_hub_2.0_android_things/config";// + NetworkUtils.getMACAddress(mContext);
            Log.i(TAG, "initializeMqttClient subscribe topic=" + mSubTopic);
            mqttClient.subscribe(mSubTopic, 1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    mReady.set(true);
}
 
開發者ID:dmtan90,項目名稱:Sense-Hub-Android-Things,代碼行數:32,代碼來源:MqttIoTPublisher.java

示例9: initializeMqttClient

import org.eclipse.paho.client.mqttv3.MqttConnectOptions; //導入方法依賴的package包/類
private void initializeMqttClient()
        throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    Log.d(TAG, "initializeMqttClient broker=" + mMqttOptions.getBrokerUrl() +
            " clientID=" + mMqttOptions.getClientId() +
            " username=" + mMqttOptions.getUsername() +
            " password=" + mMqttOptions.getPassword());
    mqttClient = new MqttClient(mMqttOptions.getBrokerUrl(),
            mMqttOptions.getClientId(), new MemoryPersistence());

    MqttConnectOptions options = new MqttConnectOptions();
    // Note that the the Google Cloud IoT only supports MQTT 3.1.1, and Paho requires that we
    // explicitly set this. If you don't set MQTT version, the server will immediately close its
    // connection to your device.
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
    options.setUserName(mMqttOptions.getUsername());
    options.setPassword(mMqttOptions.getPassword().toCharArray());

    options.setAutomaticReconnect(true);

    mqttClient.setCallback(this);
    mqttClient.connect(options);

    if(mqttClient.isConnected()) {
        try{
            Log.i(TAG, "initializeMqttClient subscribe topic=" + mMqttOptions.getSubscribeTopicName());
            mqttClient.subscribe(mMqttOptions.getSubscribeTopicName(), 1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    else{
        Log.e(TAG, "Can't connect to " + mMqttOptions.getBrokerUrl());
    }
    mReady.set(true);
}
 
開發者ID:dmtan90,項目名稱:Sense-Hub-Android-Things,代碼行數:36,代碼來源:MqttPublisher.java


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