Search in sources :

Example 51 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.

the class DDPClientImpl method unsub.

public void unsub(final TaskCompletionSource<DDPSubscription.NoSub> task, @Nullable final String id) {
    final boolean requested = sendMessage("unsub", 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).subscribe(response -> {
            String msg = extractMsg(response);
            if ("nosub".equals(msg) && response.isNull("error") && !response.isNull("id")) {
                String _id = response.optString("id");
                if (id.equals(_id)) {
                    task.setResult(new DDPSubscription.NoSub(client, id));
                    subscriptions.dispose();
                }
            }
        }, err -> {
        }));
        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)

Example 52 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.

the class DDPClientImpl method connect.

public void connect(final TaskCompletionSource<DDPClientCallback.Connect> task, final String url, String session) {
    try {
        flowable = websocket.connect(url).autoConnect();
        CompositeDisposable subscriptions = new CompositeDisposable();
        subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Open).subscribe(callback -> {
            sendMessage("connect", json -> (TextUtils.isEmpty(session) ? json : json.put("session", session)).put("version", "pre2").put("support", new JSONArray().put("pre2").put("pre1")), task);
        }, err -> {
        }));
        subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message).map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString).map(DDPClientImpl::toJson).timeout(7, TimeUnit.SECONDS).subscribe(response -> {
            String msg = extractMsg(response);
            if ("connected".equals(msg) && !response.isNull("session")) {
                task.trySetResult(new DDPClientCallback.Connect(client, response.optString("session")));
                subscriptions.dispose();
            } else if ("error".equals(msg) && "Already connected".equals(response.optString("reason"))) {
                task.trySetResult(new DDPClientCallback.Connect(client, null));
                subscriptions.dispose();
            } else if ("failed".equals(msg)) {
                task.trySetError(new DDPClientCallback.Connect.Failed(client, response.optString("version")));
                subscriptions.dispose();
            }
        }, err -> task.trySetError(new DDPClientCallback.Connect.Timeout(client))));
        addErrorCallback(subscriptions, task);
        subscribeBaseListeners();
    } catch (Exception e) {
        RCLog.e(e);
    }
}
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) JSONArray(org.json.JSONArray) RxWebSocketCallback(chat.rocket.android_ddp.rx.RxWebSocketCallback) TimeoutException(java.util.concurrent.TimeoutException) JSONException(org.json.JSONException) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Example 53 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.

the class DDPClientImpl method rpc.

public void rpc(final TaskCompletionSource<DDPClientCallback.RPC> task, String method, JSONArray params, String id, long timeoutMs) {
    final boolean requested = sendMessage("method", json -> json.put("method", method).put("params", params).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(timeoutMs, TimeUnit.MILLISECONDS).subscribe(response -> {
            String msg = extractMsg(response);
            if ("result".equals(msg)) {
                String _id = response.optString("id");
                if (id.equals(_id)) {
                    if (!response.isNull("error")) {
                        task.setError(new DDPClientCallback.RPC.Error(client, id, response.optJSONObject("error")));
                    } else {
                        String result = response.optString("result");
                        task.setResult(new DDPClientCallback.RPC(client, id, result));
                    }
                    subscriptions.dispose();
                }
            }
        }, err -> {
            if (err instanceof TimeoutException) {
                task.setError(new DDPClientCallback.RPC.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) TimeoutException(java.util.concurrent.TimeoutException)

Example 54 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable in project Rocket.Chat.Android by RocketChat.

the class DDPClientImpl method subscribeBaseListeners.

private void subscribeBaseListeners() {
    if (subscriptions != null && subscriptions.size() > 0 && !subscriptions.isDisposed()) {
        return;
    }
    subscriptions = new CompositeDisposable();
    subscriptions.add(flowable.filter(callback -> callback instanceof RxWebSocketCallback.Message).map(callback -> ((RxWebSocketCallback.Message) callback).responseBodyString).map(DDPClientImpl::toJson).subscribe(response -> {
        String msg = extractMsg(response);
        if ("ping".equals(msg)) {
            if (response.isNull("id")) {
                sendMessage("pong", null);
            } else {
                sendMessage("pong", json -> json.put("id", response.getString("id")));
            }
        }
    }, err -> {
    }));
}
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)

Example 55 with CompositeDisposable

use of io.reactivex.disposables.CompositeDisposable 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

CompositeDisposable (io.reactivex.disposables.CompositeDisposable)78 Nullable (android.support.annotation.Nullable)15 Test (org.junit.Test)11 Bundle (android.os.Bundle)10 View (android.view.View)10 Consumer (io.reactivex.functions.Consumer)10 TimeUnit (java.util.concurrent.TimeUnit)10 Flowable (io.reactivex.Flowable)9 NonNull (android.support.annotation.NonNull)8 TextUtils (android.text.TextUtils)7 ViewGroup (android.view.ViewGroup)7 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)7 TextView (android.widget.TextView)6 BindView (butterknife.BindView)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Intent (android.content.Intent)5 LayoutInflater (android.view.LayoutInflater)5 Task (bolts.Task)5 TaskCompletionSource (bolts.TaskCompletionSource)5