Search in sources :

Example 16 with TaskCompletionSource

use of bolts.TaskCompletionSource in project Rocket.Chat.Android by RocketChat.

the class DDPClientImpl method ping.

public void ping(final TaskCompletionSource<DDPClientCallback.Ping> task, @Nullable final String id) {
    final boolean requested = (TextUtils.isEmpty(id)) ? sendMessage("ping", null) : sendMessage("ping", json -> json.put("id", id));
    if (requested) {
        CompositeDisposable subscriptions = new CompositeDisposable();
        subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message).map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString).map(DDPClientImpl::toJson).timeout(4, TimeUnit.SECONDS).subscribe(response -> {
            String msg = extractMsg(response);
            if ("pong".equals(msg)) {
                if (response.isNull("id")) {
                    task.setResult(new DDPClientCallback.Ping(client, null));
                    subscriptions.dispose();
                } else {
                    String _id = response.optString("id");
                    if (id.equals(_id)) {
                        task.setResult(new DDPClientCallback.Ping(client, id));
                        subscriptions.dispose();
                    }
                }
            }
        }, err -> task.setError(new DDPClientCallback.Ping.Timeout(client))));
        addErrorCallback(subscriptions, task);
    } else {
        task.trySetError(new DDPClientCallback.Closed(client));
    }
}
Also used : RxWebSocketCallback(chat.rocket.android_ddp.rx.RxWebSocketCallback) TimeoutException(java.util.concurrent.TimeoutException) TextUtils(android.text.TextUtils) NonNull(android.support.annotation.NonNull) RCLog(chat.rocket.android.log.RCLog) TaskCompletionSource(bolts.TaskCompletionSource) TimeUnit(java.util.concurrent.TimeUnit) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) JSONException(org.json.JSONException) RxWebSocket(chat.rocket.android_ddp.rx.RxWebSocket) JSONObject(org.json.JSONObject) OkHttpClient(okhttp3.OkHttpClient) Flowable(io.reactivex.Flowable) Nullable(android.support.annotation.Nullable) Task(bolts.Task) JSONArray(org.json.JSONArray) RxWebSocketCallback(chat.rocket.android_ddp.rx.RxWebSocketCallback) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Aggregations

TaskCompletionSource (bolts.TaskCompletionSource)16 JSONException (org.json.JSONException)12 Task (bolts.Task)10 JSONObject (org.json.JSONObject)9 TextUtils (android.text.TextUtils)6 RCLog (chat.rocket.android.log.RCLog)6 RxWebSocketCallback (chat.rocket.android_ddp.rx.RxWebSocketCallback)6 TimeoutException (java.util.concurrent.TimeoutException)6 JSONArray (org.json.JSONArray)6 NonNull (android.support.annotation.NonNull)5 Nullable (android.support.annotation.Nullable)5 RxWebSocket (chat.rocket.android_ddp.rx.RxWebSocket)5 Flowable (io.reactivex.Flowable)5 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)5 TimeUnit (java.util.concurrent.TimeUnit)5 OkHttpClient (okhttp3.OkHttpClient)5 Continuation (bolts.Continuation)3 IOException (java.io.IOException)3 Capture (bolts.Capture)2 Realm (io.realm.Realm)2