本文整理汇总了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);
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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);
}
}
示例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;
}
}
示例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();
}
}
示例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);
}
示例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);
}