@@ -106,10 +106,7 @@ public enum ConnectionState {
106
106
private WebSocketConnection ws ;
107
107
private ConnectionState ddpConnectionState ;
108
108
private string sessionId ;
109
-
110
- private object subscriptionsLock = new object ( ) ;
111
- private object methodCallsLock = new object ( ) ;
112
-
109
+
113
110
private Dictionary < string , Subscription > subscriptions = new Dictionary < string , Subscription > ( ) ;
114
111
private Dictionary < string , MethodCall > methodCalls = new Dictionary < string , MethodCall > ( ) ;
115
112
@@ -158,17 +155,13 @@ public void OnWebSocketOpen()
158
155
{
159
156
OnDebugMessage ? . Invoke ( "Websocket open" ) ;
160
157
Send ( GetConnectMessage ( ) ) ;
161
- lock ( subscriptionsLock ) {
162
- foreach ( Subscription subscription in subscriptions . Values )
163
- {
164
- Send ( GetSubscriptionMessage ( subscription ) ) ;
165
- }
158
+ foreach ( Subscription subscription in subscriptions . Values )
159
+ {
160
+ Send ( GetSubscriptionMessage ( subscription ) ) ;
166
161
}
167
- lock ( methodCallsLock ) {
168
- foreach ( MethodCall methodCall in methodCalls . Values )
169
- {
170
- Send ( GetMethodCallMessage ( methodCall ) ) ;
171
- }
162
+ foreach ( MethodCall methodCall in methodCalls . Values )
163
+ {
164
+ Send ( GetMethodCallMessage ( methodCall ) ) ;
172
165
}
173
166
}
174
167
@@ -184,14 +177,8 @@ private void OnWebSocketClose(bool wasClean) {
184
177
if ( wasClean ) {
185
178
ddpConnectionState = ConnectionState . CLOSED ;
186
179
sessionId = null ;
187
- lock ( subscriptionsLock )
188
- {
189
- subscriptions . Clear ( ) ;
190
- }
191
- lock ( methodCallsLock )
192
- {
193
- methodCalls . Clear ( ) ;
194
- }
180
+ subscriptions . Clear ( ) ;
181
+ methodCalls . Clear ( ) ;
195
182
OnDisconnected ? . Invoke ( this ) ;
196
183
} else {
197
184
ddpConnectionState = ConnectionState . DISCONNECTED ;
@@ -243,10 +230,7 @@ private void HandleMessage(JSONObject message) {
243
230
244
231
case MessageType . NOSUB : {
245
232
string subscriptionId = message [ Field . ID ] . str ;
246
- lock ( subscriptionsLock )
247
- {
248
- subscriptions . Remove ( subscriptionId ) ;
249
- }
233
+ subscriptions . Remove ( subscriptionId ) ;
250
234
251
235
if ( message . HasField ( Field . ERROR ) ) {
252
236
OnError ? . Invoke ( GetError ( message [ Field . ERROR ] ) ) ;
@@ -283,10 +267,7 @@ private void HandleMessage(JSONObject message) {
283
267
284
268
foreach ( string subscriptionId in subscriptionIds ) {
285
269
Subscription subscription ;
286
- lock ( subscriptionsLock )
287
- {
288
- subscription = subscriptions [ subscriptionId ] ;
289
- }
270
+ subscription = subscriptions [ subscriptionId ] ;
290
271
if ( subscription != null ) {
291
272
subscription . isReady = true ;
292
273
subscription . OnReady ? . Invoke ( subscription ) ;
@@ -314,21 +295,14 @@ private void HandleMessage(JSONObject message) {
314
295
315
296
case MessageType . RESULT : {
316
297
string methodCallId = message [ Field . ID ] . str ;
317
- MethodCall methodCall ;
318
- lock ( methodCallsLock )
319
- {
320
- methodCall = methodCalls [ methodCallId ] ;
321
- }
298
+ MethodCall methodCall = methodCalls [ methodCallId ] ;
322
299
if ( methodCall != null ) {
323
300
if ( message . HasField ( Field . ERROR ) ) {
324
301
methodCall . error = GetError ( message [ Field . ERROR ] ) ;
325
302
}
326
303
methodCall . result = message [ Field . RESULT ] ;
327
304
if ( methodCall . hasUpdated ) {
328
- lock ( methodCallsLock )
329
- {
330
- methodCalls . Remove ( methodCallId ) ;
331
- }
305
+ methodCalls . Remove ( methodCallId ) ;
332
306
}
333
307
methodCall . hasResult = true ;
334
308
methodCall . OnResult ? . Invoke ( methodCall ) ;
@@ -339,17 +313,10 @@ private void HandleMessage(JSONObject message) {
339
313
case MessageType . UPDATED : {
340
314
string [ ] methodCallIds = ToStringArray ( message [ Field . METHODS ] ) ;
341
315
foreach ( string methodCallId in methodCallIds ) {
342
- MethodCall methodCall ;
343
- lock ( methodCallsLock )
344
- {
345
- methodCall = methodCalls [ methodCallId ] ;
346
- }
316
+ MethodCall methodCall = methodCalls [ methodCallId ] ;
347
317
if ( methodCall != null ) {
348
318
if ( methodCall . hasResult ) {
349
- lock ( methodCallsLock )
350
- {
351
- methodCalls . Remove ( methodCallId ) ;
352
- }
319
+ methodCalls . Remove ( methodCallId ) ;
353
320
}
354
321
methodCall . hasUpdated = true ;
355
322
methodCall . OnUpdated ? . Invoke ( methodCall ) ;
@@ -452,9 +419,9 @@ private string[] ToStringArray(JSONObject jo) {
452
419
return result ;
453
420
}
454
421
455
- private async Task SendAsync ( string message ) {
422
+ private void Send ( string message ) {
456
423
if ( logMessages ) OnDebugMessage ? . Invoke ( "Send: " + message ) ;
457
- await ws . Send ( message ) ;
424
+ ws . Send ( message ) ;
458
425
}
459
426
460
427
public ConnectionState GetConnectionState ( ) {
@@ -494,69 +461,33 @@ void Dispose() {
494
461
ws . Dispose ( ) ;
495
462
}
496
463
497
- // send message without waiting for its result
498
- void Send ( string message )
499
- {
500
- Task t = SendAsync ( message ) ;
501
- t . Wait ( ) ;
502
- }
503
-
504
464
public Subscription Subscribe ( string name , params JSONObject [ ] items )
505
- {
506
- Task < Subscription > sub = SubscribeAsync ( name , items ) ;
507
- // wait until message has been send
508
- sub . Wait ( ) ;
509
- return sub . Result ;
510
- }
511
-
512
- public async Task < Subscription > SubscribeAsync ( string name , params JSONObject [ ] items )
513
465
{
514
466
Subscription subscription = new Subscription ( )
515
467
{
516
468
id = "" + subscriptionId ++ ,
517
469
name = name ,
518
470
items = items
519
471
} ;
520
- lock ( subscriptions )
521
- {
522
- subscriptions [ subscription . id ] = subscription ;
523
- }
524
- await SendAsync ( GetSubscriptionMessage ( subscription ) ) ;
472
+ subscriptions [ subscription . id ] = subscription ;
473
+ Send ( GetSubscriptionMessage ( subscription ) ) ;
525
474
return subscription ;
526
475
}
527
476
528
477
public void Unsubscribe ( Subscription subscription ) {
529
478
Send ( GetUnsubscriptionMessage ( subscription ) ) ;
530
479
}
531
480
532
- public async Task < MethodCall > CallAsync ( string methodName , params JSONObject [ ] items )
533
- {
534
- MethodCall methodCall = new MethodCall ( )
535
- {
536
- id = "" + methodCallId ++ ,
537
- methodName = methodName ,
538
- items = items
539
- } ;
540
- lock ( methodCallsLock )
541
- {
542
- methodCalls [ methodCall . id ] = methodCall ;
543
- }
544
- await SendAsync ( GetMethodCallMessage ( methodCall ) ) ;
545
- return methodCall ;
546
- }
547
-
548
481
public MethodCall Call ( string methodName , params JSONObject [ ] items ) {
549
482
MethodCall methodCall = new MethodCall ( ) {
550
483
id = "" + methodCallId ++ ,
551
484
methodName = methodName ,
552
485
items = items
553
486
} ;
554
- lock ( methodCallsLock )
555
- {
556
- methodCalls [ methodCall . id ] = methodCall ;
557
- }
558
- Send ( GetMethodCallMessage ( methodCall ) ) ;
559
- return methodCall ;
487
+ methodCalls [ methodCall . id ] = methodCall ;
488
+ Send ( GetMethodCallMessage ( methodCall ) ) ;
489
+
490
+ return methodCall ;
560
491
}
561
492
562
493
void IDisposable . Dispose ( )
0 commit comments