Skip to content

Commit 3368d33

Browse files
hermanliangrogerhu
authored andcommitted
Get ws URI directly from server URL (#30)
* Get ws URI directly from server URL * extract method * minor optimization
1 parent f6e5c4d commit 3368d33

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public interface ParseLiveQueryClient {
1717

1818
class Factory {
1919

20+
public static ParseLiveQueryClient getClient() {
21+
return new ParseLiveQueryClientImpl();
22+
}
23+
2024
public static ParseLiveQueryClient getClient(URI uri) {
2125
return new ParseLiveQueryClientImpl(uri);
2226
}

ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClientImpl.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.json.JSONObject;
88

99
import java.net.URI;
10+
import java.net.URISyntaxException;
11+
import java.net.URL;
1012
import java.util.concurrent.Callable;
1113
import java.util.concurrent.Executor;
1214

@@ -31,6 +33,10 @@
3133
private int requestIdCount = 1;
3234
private boolean userInitiatedDisconnect = false;
3335

36+
/* package */ ParseLiveQueryClientImpl() {
37+
this(getDefaultUri());
38+
}
39+
3440
/* package */ ParseLiveQueryClientImpl(URI uri) {
3541
this(uri, new TubeSockWebSocketClient.TubeWebSocketClientFactory(), Task.BACKGROUND_EXECUTOR);
3642
}
@@ -45,6 +51,23 @@
4551
this.webSocketClientCallback = getWebSocketClientCallback();
4652
}
4753

54+
private static URI getDefaultUri() {
55+
URL serverUrl = ParseRESTCommand.server;
56+
if (serverUrl == null) return null;
57+
String url = serverUrl.toString();
58+
if (serverUrl.getProtocol().equals("https")) {
59+
url = url.replaceFirst("https", "wss");
60+
} else {
61+
url = url.replaceFirst("http", "ws");
62+
}
63+
try {
64+
return new URI(url);
65+
} catch (URISyntaxException e) {
66+
e.printStackTrace();
67+
throw new RuntimeException(e.getMessage());
68+
}
69+
}
70+
4871
@Override
4972
public <T extends ParseObject> SubscriptionHandling<T> subscribe(ParseQuery<T> query) {
5073
int requestId = requestIdGenerator();

0 commit comments

Comments
 (0)