|
469 | 469 | _subscription.Subscribe(); |
470 | 470 |
|
471 | 471 | // Wait for subscription to complete |
472 | | - await _subscription.ReadyAsync(); |
473 | | - await LogAsync("Subscription ready!"); |
| 472 | + try |
| 473 | + { |
| 474 | + await _subscription.ReadyAsync(); |
| 475 | + await LogAsync("Subscription ready!"); |
| 476 | + } |
| 477 | + catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout) |
| 478 | + { |
| 479 | + await LogAsync($"Subscription timed out: {ex.Message}"); |
| 480 | + return; |
| 481 | + } |
| 482 | + catch (Exception ex) |
| 483 | + { |
| 484 | + await LogAsync($"Subscription failed: {ex.Message}"); |
| 485 | + return; |
| 486 | + } |
474 | 487 |
|
475 | 488 | // === BATCHING DEMONSTRATION === |
476 | 489 | // Issue multiple API calls WITHOUT awaiting them immediately. |
|
498 | 511 |
|
499 | 512 | try |
500 | 513 | { |
501 | | - // Now await all results - they were already sent as a batch |
502 | | - await Task.WhenAll(publishTask, presenceTask, presenceStatsTask, historyTask, rpcTask); |
503 | | - |
504 | 514 | await LogAsync(""); |
505 | 515 | await LogAsync("=== BATCH RESULTS ==="); |
506 | 516 |
|
507 | 517 | // Process publish result |
508 | | - await LogAsync("✓ Publish succeeded"); |
| 518 | + try |
| 519 | + { |
| 520 | + await publishTask; |
| 521 | + await LogAsync("✓ Publish succeeded"); |
| 522 | + } |
| 523 | + catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout) |
| 524 | + { |
| 525 | + await LogAsync($"✗ Publish timed out: {ex.Message}"); |
| 526 | + } |
| 527 | + catch (Exception ex) |
| 528 | + { |
| 529 | + await LogAsync($"✗ Publish failed: {ex.Message}"); |
| 530 | + } |
509 | 531 |
|
510 | 532 | // Process presence result |
511 | | - var presence = await presenceTask; |
512 | | - await LogAsync($"✓ Presence: {presence.Clients.Count} clients"); |
513 | | - foreach (var (clientId, clientInfo) in presence.Clients) |
| 533 | + try |
| 534 | + { |
| 535 | + var presence = await presenceTask; |
| 536 | + await LogAsync($"✓ Presence: {presence.Clients.Count} clients"); |
| 537 | + foreach (var (clientId, clientInfo) in presence.Clients) |
| 538 | + { |
| 539 | + await LogAsync($" - {clientId}: user={clientInfo.User}"); |
| 540 | + } |
| 541 | + } |
| 542 | + catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout) |
514 | 543 | { |
515 | | - await LogAsync($" - {clientId}: user={clientInfo.User}"); |
| 544 | + await LogAsync($"✗ Presence timed out: {ex.Message}"); |
| 545 | + } |
| 546 | + catch (Exception ex) |
| 547 | + { |
| 548 | + await LogAsync($"✗ Presence failed: {ex.Message}"); |
516 | 549 | } |
517 | 550 |
|
518 | 551 | // Process presence stats result |
519 | | - var stats = await presenceStatsTask; |
520 | | - await LogAsync($"✓ Presence stats: {stats.NumClients} clients, {stats.NumUsers} users"); |
| 552 | + try |
| 553 | + { |
| 554 | + var stats = await presenceStatsTask; |
| 555 | + await LogAsync($"✓ Presence stats: {stats.NumClients} clients, {stats.NumUsers} users"); |
| 556 | + } |
| 557 | + catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout) |
| 558 | + { |
| 559 | + await LogAsync($"✗ Presence stats timed out: {ex.Message}"); |
| 560 | + } |
| 561 | + catch (Exception ex) |
| 562 | + { |
| 563 | + await LogAsync($"✗ Presence stats failed: {ex.Message}"); |
| 564 | + } |
521 | 565 |
|
522 | 566 | // Process history result |
523 | | - var history = await historyTask; |
524 | | - await LogAsync($"✓ History: {history.Publications.Length} messages"); |
525 | | - foreach (var pub in history.Publications.Take(3)) |
| 567 | + try |
526 | 568 | { |
527 | | - var data = Encoding.UTF8.GetString(pub.Data.Span); |
528 | | - await LogAsync($" - {data}"); |
| 569 | + var history = await historyTask; |
| 570 | + await LogAsync($"✓ History: {history.Publications.Length} messages"); |
| 571 | + foreach (var pub in history.Publications.Take(3)) |
| 572 | + { |
| 573 | + var data = Encoding.UTF8.GetString(pub.Data.Span); |
| 574 | + await LogAsync($" - {data}"); |
| 575 | + } |
| 576 | + if (history.Publications.Length > 3) |
| 577 | + { |
| 578 | + await LogAsync($" ... and {history.Publications.Length - 3} more"); |
| 579 | + } |
529 | 580 | } |
530 | | - if (history.Publications.Length > 3) |
| 581 | + catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout) |
531 | 582 | { |
532 | | - await LogAsync($" ... and {history.Publications.Length - 3} more"); |
| 583 | + await LogAsync($"✗ History timed out: {ex.Message}"); |
| 584 | + } |
| 585 | + catch (Exception ex) |
| 586 | + { |
| 587 | + await LogAsync($"✗ History failed: {ex.Message}"); |
533 | 588 | } |
534 | 589 |
|
535 | 590 | // Process RPC result |
536 | | - var rpcResult = await rpcTask; |
537 | | - var response = Encoding.UTF8.GetString(rpcResult.Data.Span); |
538 | | - await LogAsync($"✓ RPC response: {response}"); |
| 591 | + try |
| 592 | + { |
| 593 | + var rpcResult = await rpcTask; |
| 594 | + var response = Encoding.UTF8.GetString(rpcResult.Data.Span); |
| 595 | + await LogAsync($"✓ RPC response: {response}"); |
| 596 | + } |
| 597 | + catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout) |
| 598 | + { |
| 599 | + await LogAsync($"✗ RPC timed out: {ex.Message}"); |
| 600 | + } |
| 601 | + catch (Exception ex) |
| 602 | + { |
| 603 | + await LogAsync($"✗ RPC failed: {ex.Message}"); |
| 604 | + } |
539 | 605 |
|
540 | 606 | await LogAsync(""); |
541 | 607 | await LogAsync($"All 5 commands were batched and sent together!"); |
542 | 608 | await LogAsync($"Benefits: {(_useHttpStreaming ? "Reduced from 5 HTTP requests to 1!" : "Sent in quick succession via pipelining")}"); |
543 | 609 | } |
544 | 610 | catch (Exception ex) |
545 | 611 | { |
546 | | - await LogAsync($"Some operations failed: {ex.Message}"); |
547 | | - await LogAsync("Note: Presence/History may not be available on all channels"); |
| 612 | + await LogAsync($"Unexpected error: {ex.Message}"); |
548 | 613 | } |
549 | 614 |
|
550 | 615 | await LogAsync("Example completed! The connection will remain open for incoming messages."); |
|
0 commit comments