Skip to content

Commit 616c0ec

Browse files
authored
Proper timeouts, missing Client level methods, better doc comments (#13)
1 parent f549239 commit 616c0ec

File tree

5 files changed

+359
-54
lines changed

5 files changed

+359
-54
lines changed

examples/Centrifugal.Centrifuge.BlazorExample/Pages/Home.razor

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,21 @@
469469
_subscription.Subscribe();
470470

471471
// 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+
}
474487

475488
// === BATCHING DEMONSTRATION ===
476489
// Issue multiple API calls WITHOUT awaiting them immediately.
@@ -498,53 +511,105 @@
498511

499512
try
500513
{
501-
// Now await all results - they were already sent as a batch
502-
await Task.WhenAll(publishTask, presenceTask, presenceStatsTask, historyTask, rpcTask);
503-
504514
await LogAsync("");
505515
await LogAsync("=== BATCH RESULTS ===");
506516

507517
// 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+
}
509531

510532
// 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)
514543
{
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}");
516549
}
517550

518551
// 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+
}
521565

522566
// 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
526568
{
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+
}
529580
}
530-
if (history.Publications.Length > 3)
581+
catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout)
531582
{
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}");
533588
}
534589

535590
// 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+
}
539605

540606
await LogAsync("");
541607
await LogAsync($"All 5 commands were batched and sent together!");
542608
await LogAsync($"Benefits: {(_useHttpStreaming ? "Reduced from 5 HTTP requests to 1!" : "Sent in quick succession via pipelining")}");
543609
}
544610
catch (Exception ex)
545611
{
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}");
548613
}
549614

550615
await LogAsync("Example completed! The connection will remain open for incoming messages.");

examples/Centrifugal.Centrifuge.ConsoleExample/Program.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,20 @@ static async Task Main(string[] args)
132132

133133
// Publish a message (waits for subscription automatically)
134134
Console.WriteLine("\nPublishing message...");
135-
var messageData = Encoding.UTF8.GetBytes("{\"text\":\"Hello from C# SDK!\"}");
136-
await subscription.PublishAsync(messageData);
135+
try
136+
{
137+
var messageData = Encoding.UTF8.GetBytes("{\"text\":\"Hello from C# SDK!\"}");
138+
await subscription.PublishAsync(messageData);
139+
Console.WriteLine("Published message");
140+
}
141+
catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout)
142+
{
143+
Console.WriteLine($"Publish timed out: {ex.Message}");
144+
}
145+
catch (Exception ex)
146+
{
147+
Console.WriteLine($"Publish failed: {ex.Message}");
148+
}
137149

138150
// Get presence
139151
Console.WriteLine("\nFetching presence...");
@@ -149,9 +161,13 @@ static async Task Main(string[] args)
149161
var stats = await subscription.PresenceStatsAsync();
150162
Console.WriteLine($"Presence stats: {stats.NumClients} clients, {stats.NumUsers} users");
151163
}
164+
catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout)
165+
{
166+
Console.WriteLine($"Presence timed out: {ex.Message}");
167+
}
152168
catch (Exception ex)
153169
{
154-
Console.WriteLine($"Presence not available: {ex.Message}");
170+
Console.WriteLine($"Presence failed: {ex.Message}");
155171
}
156172

157173
// Get history
@@ -171,9 +187,13 @@ static async Task Main(string[] args)
171187
Console.WriteLine($" - {data}");
172188
}
173189
}
190+
catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout)
191+
{
192+
Console.WriteLine($"History timed out: {ex.Message}");
193+
}
174194
catch (Exception ex)
175195
{
176-
Console.WriteLine($"History not available: {ex.Message}");
196+
Console.WriteLine($"History failed: {ex.Message}");
177197
}
178198

179199
// RPC example
@@ -185,6 +205,10 @@ static async Task Main(string[] args)
185205
var response = Encoding.UTF8.GetString(rpcResult.Data.Span);
186206
Console.WriteLine($"RPC response: {response}");
187207
}
208+
catch (CentrifugeException ex) when (ex.Code == CentrifugeErrorCodes.Timeout)
209+
{
210+
Console.WriteLine($"RPC timed out: {ex.Message}");
211+
}
188212
catch (Exception ex)
189213
{
190214
Console.WriteLine($"RPC failed: {ex.Message}");

src/Centrifugal.Centrifuge/Centrifugal.Centrifuge.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<!-- Package Information -->
1717
<PackageId>Centrifugal.Centrifuge</PackageId>
18-
<Version>0.0.10</Version>
18+
<Version>0.0.11</Version>
1919
<Authors>Centrifugal Labs</Authors>
2020
<Company>Centrifugal Labs</Company>
2121
<Description>

0 commit comments

Comments
 (0)