Skip to content

Commit 542e3cb

Browse files
committed
Subscription bug fixes
1 parent 8740db2 commit 542e3cb

File tree

5 files changed

+25
-31
lines changed

5 files changed

+25
-31
lines changed

Parse/Infrastructure/Execution/TextWebSocketClient.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,9 @@ public async Task OpenAsync(string serverUri, CancellationToken cancellationToke
5454
{
5555
_webSocket ??= new ClientWebSocket();
5656

57-
Debug.WriteLine($"Status: {_webSocket.State.ToString()}");
5857
if (_webSocket.State != WebSocketState.Open && _webSocket.State != WebSocketState.Connecting)
5958
{
60-
Debug.WriteLine($"Connecting to: {serverUri}");
6159
await _webSocket.ConnectAsync(new Uri(serverUri), cancellationToken);
62-
Debug.WriteLine($"Status: {_webSocket.State.ToString()}");
6360
StartListening(cancellationToken);
6461
}
6562
}
@@ -141,11 +138,9 @@ private void StartListening(CancellationToken cancellationToken)
141138
/// </returns>
142139
public async Task SendAsync(string message, CancellationToken cancellationToken = default)
143140
{
144-
Debug.WriteLine($"Sending: {message}");
145141
if (_webSocket is not null && _webSocket.State == WebSocketState.Open)
146142
{
147143
await _webSocket.SendAsync(Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, cancellationToken);
148-
Console.WriteLine("Sent");
149144
}
150145
}
151146
}

Parse/Platform/LiveQueries/ParseLiveQuery.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ParseLiveQuery<T> where T : ParseObject
2121
/// <summary>
2222
/// Serialized <see langword="where"/> clauses.
2323
/// </summary>
24-
string Filters { get; }
24+
IDictionary<string, object> Filters { get; }
2525

2626
/// <summary>
2727
/// Serialized key selections.
@@ -39,11 +39,11 @@ public class ParseLiveQuery<T> where T : ParseObject
3939

4040
private int RequestId = 0;
4141

42-
public ParseLiveQuery(IServiceHub serviceHub, string className, object filters, IEnumerable<string> selectedKeys = null, IEnumerable<string> watchedKeys = null)
42+
public ParseLiveQuery(IServiceHub serviceHub, string className, IDictionary<string, object> filters, IEnumerable<string> selectedKeys = null, IEnumerable<string> watchedKeys = null)
4343
{
4444
Services = serviceHub;
4545
ClassName = className;
46-
Filters = JsonUtilities.Encode(filters);
46+
Filters = filters;
4747

4848
if (selectedKeys is not null)
4949
{
@@ -91,17 +91,15 @@ internal ParseLiveQuery(ParseLiveQuery<T> source, IEnumerable<string> watchedKey
9191
/// <returns>A new query with the additional constraint.</returns>
9292
public ParseLiveQuery<T> Watch(string watch) => new(this, new List<string> { watch });
9393

94-
internal IDictionary<string, string> BuildParameters(bool includeClassName = false)
94+
internal IDictionary<string, object> BuildParameters()
9595
{
96-
Dictionary<string, string> result = new Dictionary<string, string>();
97-
if (Filters != null)
98-
result["where"] = Filters;
96+
Dictionary<string, object> result = new Dictionary<string, object>();
97+
result["className"] = ClassName;
98+
result["where"] = Filters;
9999
if (KeySelections != null)
100-
result["keys"] = String.Join(",", KeySelections.ToArray());
100+
result["keys"] = KeySelections.ToArray();
101101
if (KeyWatchers != null)
102-
result["watch"] = String.Join(",", KeyWatchers.ToArray());
103-
if (includeClassName)
104-
result["className"] = ClassName;
102+
result["watch"] = KeyWatchers.ToArray();
105103
return result;
106104
}
107105

Parse/Platform/LiveQueries/ParseLiveQueryController.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public enum ParseLiveQueryState
9898
/// This constructor is used to initialize a new instance of the <see cref="ParseLiveQueryController"/> class
9999
public ParseLiveQueryController(IWebSocketClient webSocketClient)
100100
{
101-
Debug.WriteLine("ParseLiveQueryController initialized.");
102101
WebSocketClient = webSocketClient;
103102
State = ParseLiveQueryState.Closed;
104103
}
@@ -227,16 +226,19 @@ private void ProcessMessage(IDictionary<string, object> message)
227226
}
228227
}
229228

230-
private IDictionary<string, object> AppendSessionToken(IDictionary<string, object> message)
229+
private async Task<IDictionary<string, object>> AppendSessionToken(IDictionary<string, object> message)
231230
{
232-
return message.Concat(new Dictionary<string, object> {
233-
{ "sessionToken", ParseClient.Instance.Services.GetCurrentSessionToken() }
234-
}).ToDictionary();
231+
string sessionToken = await ParseClient.Instance.Services.GetCurrentSessionToken();
232+
return sessionToken is null
233+
? message
234+
: message.Concat(new Dictionary<string, object> {
235+
{ "sessionToken", await ParseClient.Instance.Services.GetCurrentSessionToken() }
236+
}).ToDictionary();
235237
}
236238

237239
private async Task SendMessage(IDictionary<string, object> message, CancellationToken cancellationToken)
238240
{
239-
await WebSocketClient.SendAsync(JsonUtilities.Encode(AppendSessionToken(message)), cancellationToken);
241+
await WebSocketClient.SendAsync(JsonUtilities.Encode(message), cancellationToken);
240242
}
241243

242244
private async Task OpenAsync(CancellationToken cancellationToken = default)
@@ -283,15 +285,15 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
283285
{ "applicationId", ParseClient.Instance.Services.LiveQueryServerConnectionData.ApplicationID },
284286
{ "windowsKey", ParseClient.Instance.Services.LiveQueryServerConnectionData.Key }
285287
};
286-
await SendMessage(message, cancellationToken);
288+
await SendMessage(await AppendSessionToken(message), cancellationToken);
287289
ConnectionSignal = new CancellationTokenSource();
288290
bool signalReceived = ConnectionSignal.Token.WaitHandle.WaitOne(TimeOut);
289-
State = ParseLiveQueryState.Connected;
290-
ConnectionSignal.Dispose();
291291
if (!signalReceived)
292292
{
293293
throw new TimeoutException();
294294
}
295+
State = ParseLiveQueryState.Connected;
296+
ConnectionSignal.Dispose();
295297
}
296298
else if (State == ParseLiveQueryState.Connecting)
297299
{
@@ -339,9 +341,9 @@ public async Task<IParseLiveQuerySubscription> SubscribeAsync<T>(ParseLiveQuery<
339341
{
340342
{ "op", "subscribe" },
341343
{ "requestId", requestId },
342-
{ "query", liveQuery.BuildParameters(true) }
344+
{ "query", liveQuery.BuildParameters() }
343345
};
344-
await SendMessage(message, cancellationToken);
346+
await SendMessage(await AppendSessionToken(message), cancellationToken);
345347
CancellationTokenSource completionSignal = new CancellationTokenSource();
346348
SubscriptionSignals.Add(requestId, completionSignal);
347349
bool signalReceived = completionSignal.Token.WaitHandle.WaitOne(TimeOut);
@@ -381,9 +383,9 @@ public async Task UpdateSubscriptionAsync<T>(ParseLiveQuery<T> liveQuery, int re
381383
{
382384
{ "op", "update" },
383385
{ "requestId", requestId },
384-
{ "query", liveQuery.BuildParameters(true) }
386+
{ "query", liveQuery.BuildParameters() }
385387
};
386-
await SendMessage(message, cancellationToken);
388+
await SendMessage(await AppendSessionToken(message), cancellationToken);
387389
}
388390

389391
/// <summary>

Parse/Platform/Queries/ParseQuery.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,6 @@ public override int GetHashCode()
915915
public ParseLiveQuery<T> GetLive()
916916
{
917917
IDictionary<string, object> paramsDict = BuildParameters();
918-
return new ParseLiveQuery<T>(Services, ClassName, paramsDict["where"], paramsDict.TryGetValue("keys", out object selectedKey) ? selectedKey as IEnumerable<string> : null);
918+
return new ParseLiveQuery<T>(Services, ClassName, paramsDict["where"] as IDictionary<string, object>, KeySelections);
919919
}
920920
}

Parse/Platform/Queries/ParseQueryController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ internal class ParseQueryController : IParseQueryController
2424

2525
public ParseQueryController(IParseCommandRunner commandRunner, IParseDataDecoder decoder)
2626
{
27-
Debug.WriteLine("ParseQueryController initialized");
2827
CommandRunner = commandRunner;
2928
Decoder = decoder;
3029
}

0 commit comments

Comments
 (0)