From db767c0242e6aa0569d9d36ed847868f36d7446c Mon Sep 17 00:00:00 2001 From: Herman Liang Date: Sun, 26 Mar 2017 22:01:46 +0800 Subject: [PATCH 1/3] Get ws URI directly from server URL --- .../java/com/parse/ParseLiveQueryClient.java | 4 +++ .../com/parse/ParseLiveQueryClientImpl.java | 27 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClient.java b/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClient.java index e6b9e8b..3b30238 100644 --- a/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClient.java +++ b/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClient.java @@ -17,6 +17,10 @@ public interface ParseLiveQueryClient { class Factory { + public static ParseLiveQueryClient getClient() { + return new ParseLiveQueryClientImpl(); + } + public static ParseLiveQueryClient getClient(URI uri) { return new ParseLiveQueryClientImpl(uri); } diff --git a/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java b/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java index b8970ed..56d5969 100644 --- a/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java +++ b/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java @@ -7,6 +7,8 @@ import org.json.JSONObject; import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.util.concurrent.Callable; import java.util.concurrent.Executor; @@ -23,7 +25,7 @@ private final String applicationId; private final String clientKey; private final SparseArray> subscriptions = new SparseArray<>(); - private final URI uri; + private URI uri; private final WebSocketClientFactory webSocketClientFactory; private final WebSocketClient.WebSocketClientCallback webSocketClientCallback; @@ -31,6 +33,29 @@ private int requestIdCount = 1; private boolean userInitiatedDisconnect = false; + /* package */ ParseLiveQueryClientImpl() { + this(null); + URL serverUrl = ParseRESTCommand.server; + if (serverUrl == null) { + throw new RuntimeException("serverUrl is null. " + + "You must call Parse.initialize(Context)" + + " before using the Parse LiveQuery library."); + } else { + String url = serverUrl.toString(); + if (serverUrl.getProtocol().equals("https")) { + url = url.replaceFirst("https", "wss"); + } else { + url = url.replaceFirst("http", "ws"); + } + try { + this.uri = new URI(url); + } catch (URISyntaxException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } + } + } + /* package */ ParseLiveQueryClientImpl(URI uri) { this(uri, new TubeSockWebSocketClient.TubeWebSocketClientFactory(), Task.BACKGROUND_EXECUTOR); } From 1040abb9aff40593ee277adcc144da613f1418ab Mon Sep 17 00:00:00 2001 From: Herman Liang Date: Mon, 27 Mar 2017 08:58:30 +0800 Subject: [PATCH 2/3] extract method --- .../com/parse/ParseLiveQueryClientImpl.java | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java b/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java index 56d5969..529bd37 100644 --- a/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java +++ b/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java @@ -25,7 +25,7 @@ private final String applicationId; private final String clientKey; private final SparseArray> subscriptions = new SparseArray<>(); - private URI uri; + private final URI uri; private final WebSocketClientFactory webSocketClientFactory; private final WebSocketClient.WebSocketClientCallback webSocketClientCallback; @@ -34,26 +34,7 @@ private boolean userInitiatedDisconnect = false; /* package */ ParseLiveQueryClientImpl() { - this(null); - URL serverUrl = ParseRESTCommand.server; - if (serverUrl == null) { - throw new RuntimeException("serverUrl is null. " - + "You must call Parse.initialize(Context)" - + " before using the Parse LiveQuery library."); - } else { - String url = serverUrl.toString(); - if (serverUrl.getProtocol().equals("https")) { - url = url.replaceFirst("https", "wss"); - } else { - url = url.replaceFirst("http", "ws"); - } - try { - this.uri = new URI(url); - } catch (URISyntaxException e) { - e.printStackTrace(); - throw new RuntimeException(e.getMessage()); - } - } + this(getDefaultUri()); } /* package */ ParseLiveQueryClientImpl(URI uri) { @@ -70,6 +51,25 @@ this.webSocketClientCallback = getWebSocketClientCallback(); } + private static URI getDefaultUri() { + URL serverUrl = ParseRESTCommand.server; + if (serverUrl != null) { + String url = serverUrl.toString(); + if (serverUrl.getProtocol().equals("https")) { + url = url.replaceFirst("https", "wss"); + } else { + url = url.replaceFirst("http", "ws"); + } + try { + return new URI(url); + } catch (URISyntaxException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); + } + } + return null; + } + @Override public SubscriptionHandling subscribe(ParseQuery query) { int requestId = requestIdGenerator(); From d50da488557b31c5b9a0c28a69ace1f32a2e6f8b Mon Sep 17 00:00:00 2001 From: Herman Liang Date: Mon, 27 Mar 2017 23:54:48 +0800 Subject: [PATCH 3/3] minor optimization --- .../com/parse/ParseLiveQueryClientImpl.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java b/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java index 529bd37..f62e802 100644 --- a/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java +++ b/ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java @@ -53,21 +53,19 @@ private static URI getDefaultUri() { URL serverUrl = ParseRESTCommand.server; - if (serverUrl != null) { - String url = serverUrl.toString(); - if (serverUrl.getProtocol().equals("https")) { - url = url.replaceFirst("https", "wss"); - } else { - url = url.replaceFirst("http", "ws"); - } - try { - return new URI(url); - } catch (URISyntaxException e) { - e.printStackTrace(); - throw new RuntimeException(e.getMessage()); - } + if (serverUrl == null) return null; + String url = serverUrl.toString(); + if (serverUrl.getProtocol().equals("https")) { + url = url.replaceFirst("https", "wss"); + } else { + url = url.replaceFirst("http", "ws"); + } + try { + return new URI(url); + } catch (URISyntaxException e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage()); } - return null; } @Override