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));
}
}
Aggregations