Search in sources :

Example 1 with DisconnectedBufferOptions

use of org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions in project Tusky by Vavassor.

the class PushNotificationClient method connect.

/** Connect to the MQTT broker. */
public void connect(Context context) {
    MqttConnectOptions options = new MqttConnectOptions();
    options.setAutomaticReconnect(true);
    options.setCleanSession(false);
    try {
        String password = context.getString(R.string.tusky_api_keystore_password);
        InputStream keystore = context.getResources().openRawResource(R.raw.keystore_tusky_api);
        try {
            options.setSocketFactory(mqttAndroidClient.getSSLSocketFactory(keystore, password));
        } finally {
            IOUtils.closeQuietly(keystore);
        }
        mqttAndroidClient.connect(options).setActionCallback(new IMqttActionListener() {

            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                DisconnectedBufferOptions bufferOptions = new DisconnectedBufferOptions();
                bufferOptions.setBufferEnabled(true);
                bufferOptions.setBufferSize(100);
                bufferOptions.setPersistBuffer(false);
                bufferOptions.setDeleteOldestMessages(false);
                mqttAndroidClient.setBufferOpts(bufferOptions);
                onConnectionSuccess();
                flushQueuedActions();
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                Log.e(TAG, "An exception occurred while connecting. " + exception.getMessage() + " " + exception.getCause());
                onConnectionFailure();
            }
        });
    } catch (MqttException e) {
        Log.e(TAG, "An exception occurred while connecpting. " + e.getMessage());
        onConnectionFailure();
    }
}
Also used : IMqttActionListener(org.eclipse.paho.client.mqttv3.IMqttActionListener) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) InputStream(java.io.InputStream) MqttException(org.eclipse.paho.client.mqttv3.MqttException) DisconnectedBufferOptions(org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions) IMqttToken(org.eclipse.paho.client.mqttv3.IMqttToken)

Aggregations

InputStream (java.io.InputStream)1 DisconnectedBufferOptions (org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions)1 IMqttActionListener (org.eclipse.paho.client.mqttv3.IMqttActionListener)1 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MqttException (org.eclipse.paho.client.mqttv3.MqttException)1