Skip to content

Commit 2be2bfd

Browse files
author
Serhii Yolkin
committed
Fixed an issue that caused timeouts when multiple calls were going on at the same time
1 parent f5f8d00 commit 2be2bfd

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

UnityProject/Assets/LoomSDK/Source/Runtime/Internal/WebSocketRpcClient.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public override Task DisconnectAsync()
149149
return tcs.Task;
150150
}
151151

152-
public override Task SubscribeAsync(EventHandler<JsonRpcEventData> handler, ICollection<string> topics = null)
152+
public override Task SubscribeAsync(EventHandler<JsonRpcEventData> handler, ICollection<string> topics)
153153
{
154154
var isFirstSub = this.eventReceived == null;
155155
this.eventReceived += handler;
@@ -159,14 +159,14 @@ public override Task SubscribeAsync(EventHandler<JsonRpcEventData> handler, ICol
159159
}
160160
// TODO: once re-sub on reconnect is implemented this should only
161161
// be done on first sub
162-
Dictionary<string, ICollection<string>> result = null;
163-
if (topics != null)
162+
Dictionary<string, ICollection<string>> args = null;
163+
if (topics != null && topics.Count > 0)
164164
{
165-
result = new Dictionary<string, ICollection<string>>();
166-
result.Add("topics", topics);
165+
args = new Dictionary<string, ICollection<string>>();
166+
args.Add("topics", topics);
167167
}
168168

169-
return SendAsync<object, Dictionary<string, ICollection<string>>>("subevents", result);
169+
return SendAsync<object, Dictionary<string, ICollection<string>>>("subevents", args);
170170
}
171171

172172
public override Task UnsubscribeAsync(EventHandler<JsonRpcEventData> handler)
@@ -180,31 +180,30 @@ public override Task UnsubscribeAsync(EventHandler<JsonRpcEventData> handler)
180180
return Task.CompletedTask;
181181
}
182182

183-
public override async Task<T> SendAsync<T, U>(string method, U args)
183+
public override async Task<TResult> SendAsync<TResult, TArgs>(string method, TArgs args)
184184
{
185-
var tcs = new TaskCompletionSource<T>();
185+
var tcs = new TaskCompletionSource<TResult>();
186186
var msgId = Guid.NewGuid().ToString();
187187
EventHandler<CloseEventArgs> closeHandler = null;
188188
EventHandler<MessageEventArgs> messageHandler = null;
189189
closeHandler = (sender, e) =>
190190
{
191191
tcs.TrySetException(new RpcClientException($"WebSocket closed unexpectedly with error {e.Code}: {e.Reason}"));
192192
};
193-
193+
194194
messageHandler = (sender, e) =>
195195
{
196-
this.webSocket.OnClose -= closeHandler;
197-
this.webSocket.OnMessage -= messageHandler;
198-
199196
try
200197
{
201-
// TODO: set a timeout and throw exception when it's exceeded
198+
// TODO: implement a more optimal way to handle data. Currently, each handler deserializes the payload independently,
199+
// which means that if 20 simultaneous calls are made, up to 20 * 20 = 400 total deserializations can be made
202200
if (e.IsText && !string.IsNullOrEmpty(e.Data))
203201
{
204202
this.Logger.Log("[Response Data] " + e.Data);
205203
var partialMsg = JsonConvert.DeserializeObject<JsonRpcResponse>(e.Data);
206204
if (partialMsg.Id == msgId)
207205
{
206+
this.webSocket.OnClose -= closeHandler;
208207
this.webSocket.OnMessage -= messageHandler;
209208
if (partialMsg.Error != null)
210209
{
@@ -214,7 +213,7 @@ public override async Task<T> SendAsync<T, U>(string method, U args)
214213
));
215214
}
216215

217-
var fullMsg = JsonConvert.DeserializeObject<JsonRpcResponse<T>>(e.Data);
216+
var fullMsg = JsonConvert.DeserializeObject<JsonRpcResponse<TResult>>(e.Data);
218217
tcs.TrySetResult(fullMsg.Result);
219218
}
220219
}

0 commit comments

Comments
 (0)