Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #21: connect and disconnect on the background executor #22

Merged
merged 3 commits into from
Mar 23, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,21 @@ public void unsubscribe(final ParseQuery<T> query, final SubscriptionHandling su

@Override
public void reconnect() {
if (webSocketClient != null) {
webSocketClient.close();
}
this.webSocketClient = webSocketClientFactory.createInstance(webSocketClientCallback, uri);
this.webSocketClient.open();
disconnectAsync().continueWith(new Continuation<Void, Void>() {
@Override
public Void then(Task<Void> task) throws Exception {
webSocketClient = webSocketClientFactory.createInstance(webSocketClientCallback, uri);
webSocketClient.open();
return null;
}
});
userInitiatedDisconnect = false;
}

@Override
public void disconnect() {
if (webSocketClient != null) {
webSocketClient.close();
webSocketClient = null;
disconnectAsync();
userInitiatedDisconnect = true;
}
}
Expand Down Expand Up @@ -131,6 +133,20 @@ public Void call() throws Exception {
}, taskExecutor);
}

private Task<Void> disconnectAsync() {
return Task.call(new Callable<Void>() {
@Override
public Void call() throws Exception {
if (webSocketClient != null) {
webSocketClient.close();
webSocketClient = null;
}

return null;
}
}, taskExecutor);
}

private void parseMessage(String message) throws LiveQueryException {
try {
JSONObject jsonObject = new JSONObject(message);
Expand Down Expand Up @@ -227,7 +243,7 @@ private void sendSubscription(final Subscription<T> subscription) {
public Void then(Task<String> task) throws Exception {
String sessionToken = task.getResult();
SubscribeClientOperation<T> op = new SubscribeClientOperation<>(subscription.getRequestId(), subscription.getQueryState(), sessionToken);

// dispatch errors
sendOperationAsync(op).continueWith(new Continuation<Void, Void>() {
public Void then(Task<Void> task) {
Expand Down