DDPClient.java 2.18 KB
Newer Older
1 2 3
package chat.rocket.android_ddp;

import android.support.annotation.Nullable;
4

Yusuke Iwaki's avatar
Yusuke Iwaki committed
5 6
import org.json.JSONArray;

7 8 9
import bolts.Task;
import bolts.TaskCompletionSource;
import chat.rocket.android_ddp.rx.RxWebSocketCallback;
10 11
import io.reactivex.Flowable;
import io.reactivex.Maybe;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import okhttp3.OkHttpClient;

public class DDPClient {
  // reference: https://github.com/eddflrs/meteor-ddp/blob/master/meteor-ddp.js

  private final DDPClientImpl impl;

  public DDPClient(OkHttpClient client) {
    impl = new DDPClientImpl(this, client);
  }

  public Task<DDPClientCallback.Connect> connect(String url) {
    return connect(url, null);
  }

  public Task<DDPClientCallback.Connect> connect(String url, String session) {
    TaskCompletionSource<DDPClientCallback.Connect> task = new TaskCompletionSource<>();
    impl.connect(task, url, session);
    return task.getTask();
  }

  public Task<DDPClientCallback.Ping> ping(@Nullable String id) {
    TaskCompletionSource<DDPClientCallback.Ping> task = new TaskCompletionSource<>();
    impl.ping(task, id);
    return task.getTask();
  }

39
  public Maybe<DDPClientCallback.Base> doPing(@Nullable String id) {
40 41 42
    return impl.ping(id);
  }

43
  public Task<DDPClientCallback.RPC> rpc(String method, JSONArray params, String id,
Yusuke Iwaki's avatar
Yusuke Iwaki committed
44
                                         long timeoutMs) {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    TaskCompletionSource<DDPClientCallback.RPC> task = new TaskCompletionSource<>();
    impl.rpc(task, method, params, id, timeoutMs);
    return task.getTask();
  }

  public Task<DDPSubscription.Ready> sub(String id, String name, JSONArray params) {
    TaskCompletionSource<DDPSubscription.Ready> task = new TaskCompletionSource<>();
    impl.sub(task, name, params, id);
    return task.getTask();
  }

  public Task<DDPSubscription.NoSub> unsub(String id) {
    TaskCompletionSource<DDPSubscription.NoSub> task = new TaskCompletionSource<>();
    impl.unsub(task, id);
    return task.getTask();
  }

62
  public Flowable<DDPSubscription.Event> getSubscriptionCallback() {
63 64 65 66 67 68 69 70 71 72 73
    return impl.getDDPSubscription();
  }

  public Task<RxWebSocketCallback.Close> getOnCloseCallback() {
    return impl.getOnCloseCallback();
  }

  public void close() {
    impl.close(1000, "closed by DDPClient#close()");
  }
}