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


Java MqttClient.isConnected方法代碼示例

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


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

示例1: MqttClientKetiSub

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public MqttClientKetiSub(String serverUrl) {
	
	this.mqttServerUrl = serverUrl;
	
	System.out.println("[KETI MQTT Client] Client Initialize");
	
	try {
		mqc = new MqttClient(mqttServerUrl, mqttClientId, persistence);
		
		while(!mqc.isConnected()){
			mqc.connect();
			System.out.println("[KETI MQTT Client] Connection try");
		}
		
		System.out.println("[KETI MQTT Client] Connected to Server - " + mqttServerUrl);
	} catch (MqttException e) {
		e.printStackTrace();
	}
}
 
開發者ID:IoTKETI,項目名稱:nCube-Thyme-Java,代碼行數:20,代碼來源:MqttClientKetiSub.java

示例2: MqttClientKetiPub

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public MqttClientKetiPub(String serverUrl) {
	
	this.mqttServerUrl = serverUrl;
	
	System.out.println("[KETI MQTT Client] Client Initialize");
	
	try {
		mqc = new MqttClient(mqttServerUrl, mqttClientId, persistence);
		
		while(!mqc.isConnected()){
			mqc.connect();
			System.out.println("[KETI MQTT Client] Connection try");
		}
		
		System.out.println("[KETI MQTT Client] Connected to Server - " + mqttServerUrl);
	} catch (MqttException e) {
		e.printStackTrace();
	}
}
 
開發者ID:IoTKETI,項目名稱:nCube-Thyme-Java,代碼行數:20,代碼來源:MqttClientKetiPub.java

示例3: connectPublish

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void connectPublish(final String userKeyString, final String deviceKeyString, final String status) {

        try {
            final MqttClient client = connect();
            if (client == null || !client.isConnected()) {
                return;
            }
            MqttMessage message = new MqttMessage();
            message.setRetained(false);
            message.setPayload((userKeyString + "," + deviceKeyString + "," + status).getBytes());
            Utils.printLog(context,TAG, "UserKeyString,DeviceKeyString,status:" + userKeyString + "," + deviceKeyString + "," + status);
            message.setQos(0);
            client.publish(STATUS, message);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:19,代碼來源:ApplozicMqttService.java

示例4: subscribe

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void subscribe() {
    if (!Utils.isInternetAvailable(context)) {
        return;
    }
    final String deviceKeyString = MobiComUserPreference.getInstance(context).getDeviceKeyString();
    final String userKeyString = MobiComUserPreference.getInstance(context).getSuUserKeyString();
    if (TextUtils.isEmpty(deviceKeyString) || TextUtils.isEmpty(userKeyString)) {
        return;
    }
    try {
        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }
        connectPublish(userKeyString, deviceKeyString, "1");
        subscribeToConversation();
        if (client != null) {
            client.setCallback(ApplozicMqttService.this);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:24,代碼來源:ApplozicMqttService.java

示例5: publishTopic

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void publishTopic(final String applicationId, final String status, final String loggedInUserId, final String userId) {
    try {
        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }
        MqttMessage message = new MqttMessage();
        message.setRetained(false);
        message.setPayload((applicationId + "," + loggedInUserId + "," + status).getBytes());
        message.setQos(0);
        client.publish("typing" + "-" + applicationId + "-" + userId, message);
        Utils.printLog(context,TAG, "Published " + new String(message.getPayload()) + " to topic: " + "typing" + "-" + applicationId + "-" + userId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:17,代碼來源:ApplozicMqttService.java

示例6: subscribeToTypingTopic

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void subscribeToTypingTopic(Channel channel) {
    try {
        String currentId = null;
        if (channel != null) {
            currentId = String.valueOf(channel.getKey());
        } else {
            MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
            currentId = mobiComUserPreference.getUserId();
        }

        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }

        client.subscribe("typing-" + getApplicationKey(context) + "-" + currentId, 0);
        Utils.printLog(context,TAG, "Subscribed to topic: " + "typing-" + getApplicationKey(context) + "-" + currentId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:22,代碼來源:ApplozicMqttService.java

示例7: unSubscribeToTypingTopic

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void unSubscribeToTypingTopic(Channel channel) {
    try {
        String currentId = null;
        if (channel != null) {
            currentId = String.valueOf(channel.getKey());
        } else {
            MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
            currentId = mobiComUserPreference.getUserId();
        }

        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }

        client.unsubscribe("typing-" + getApplicationKey(context) + "-" + currentId);
        Utils.printLog(context,TAG, "UnSubscribed to topic: " + "typing-" + getApplicationKey(context) + "-" + currentId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:22,代碼來源:ApplozicMqttService.java

示例8: connectPublish

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void connectPublish(final String userKeyString, final String deviceKeyString, final String status) {

        try {
            final MqttClient client = connect();
            if (client == null || !client.isConnected()) {
                return;
            }
            MqttMessage message = new MqttMessage();
            message.setRetained(false);
            message.setPayload((userKeyString + "," + deviceKeyString + "," + status).getBytes());
            Utils.printLog(context, TAG, "UserKeyString,DeviceKeyString,status:" + userKeyString + "," + deviceKeyString + "," + status);
            message.setQos(0);
            client.publish(STATUS, message);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:19,代碼來源:ApplozicMqttService.java

示例9: publishTopic

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void publishTopic(final String applicationId, final String status, final String loggedInUserId, final String userId) {
    try {
        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }
        MqttMessage message = new MqttMessage();
        message.setRetained(false);
        message.setPayload((applicationId + "," + loggedInUserId + "," + status).getBytes());
        message.setQos(0);
        client.publish("typing" + "-" + applicationId + "-" + userId, message);
        Utils.printLog(context, TAG, "Published " + new String(message.getPayload()) + " to topic: " + "typing" + "-" + applicationId + "-" + userId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:17,代碼來源:ApplozicMqttService.java

示例10: subscribeToTypingTopic

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void subscribeToTypingTopic(Channel channel) {
    try {
        String currentId = null;
        if (channel != null) {
            currentId = String.valueOf(channel.getKey());
        } else {
            MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
            currentId = mobiComUserPreference.getUserId();
        }

        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }

        client.subscribe("typing-" + getApplicationKey(context) + "-" + currentId, 0);
        Utils.printLog(context, TAG, "Subscribed to topic: " + "typing-" + getApplicationKey(context) + "-" + currentId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:22,代碼來源:ApplozicMqttService.java

示例11: unSubscribeToTypingTopic

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
public synchronized void unSubscribeToTypingTopic(Channel channel) {
    try {
        String currentId = null;
        if (channel != null) {
            currentId = String.valueOf(channel.getKey());
        } else {
            MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
            currentId = mobiComUserPreference.getUserId();
        }

        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }

        client.unsubscribe("typing-" + getApplicationKey(context) + "-" + currentId);
        Utils.printLog(context, TAG, "UnSubscribed to topic: " + "typing-" + getApplicationKey(context) + "-" + currentId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
開發者ID:AppLozic,項目名稱:Applozic-Android-Chat-Sample,代碼行數:22,代碼來源:ApplozicMqttService.java

示例12: initializeMqttClient

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的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

示例13: initializeMqttClient

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的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

示例14: close

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
private void close(MqttClient client){
	try {
		if(client.isConnected())
			client.disconnect();
		logger.info("client disconnected! clientID:"+client.getClientId());
	} catch (MqttException e) {
		e.printStackTrace();
	}
}
 
開發者ID:projectsrepos,項目名稱:jim,代碼行數:10,代碼來源:MqttPerformanceClient.java

示例15: doConnect

import org.eclipse.paho.client.mqttv3.MqttClient; //導入方法依賴的package包/類
@Override
protected synchronized MqttClient doConnect(MqttClient client) throws MqttException {
    MqttConfig config = configFn.get();
    
    if (client == null)
        client = newClient(config);
    
    if (client.isConnected())
        return client;
    
    MqttConnectOptions options = (MqttConnectOptions) config.options();
    
    logger.info("{} cleanSession:{} userName:{} password:{} idleTimeout:{} idleReconnectTimeout:{} cnTimeout:{} keepalive:{} serverURIs:{} willDst:{} willMsg:{}",
            id(),
            options.isCleanSession(),
            options.getUserName(),
            options.getPassword() == null ? null : "*****",
            config.getIdleTimeout(),
            config.getSubscriberIdleReconnectInterval(),
            options.getConnectionTimeout(),
            options.getKeepAliveInterval(),
            options.getServerURIs(),
            options.getWillDestination(),
            options.getWillMessage()
            );
    
    client.connect(options);

    setIdleTimeout(config.getIdleTimeout(), TimeUnit.SECONDS);
    
    MqttSubscriber<?> sub = subscriber;
    if (sub != null) {
        setIdleReconnectInterval(config.getSubscriberIdleReconnectInterval());
        sub.connected(client);
    }

    return client;
}
 
開發者ID:quarks-edge,項目名稱:quarks,代碼行數:39,代碼來源:MqttConnector.java


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