当前位置: 首页>>代码示例>>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;未经允许,请勿转载。