4.0 KiB
ConnectionManager.cs
Source:
src/EchoHub.Client/Services/ConnectionManager.cs
Contents
ConnectionManager
File:
src/EchoHub.Client/Services/ConnectionManager.cs
Kind: class
internal sealed class ConnectionManager : IAsyncDisposable
Manages a server connection end-to-end: handles authentication via ApiClient, establishes end-to-end encryption, creates and wires an EchoHubConnection, tracks joined channels, and exposes SignalR events so higher-level orchestrators can react without touching connection internals. Reach for ConnectionManager when you want UI code (for example an AppOrchestrator) to observe connection and chat events through simple events rather than managing ApiClient and EchoHubConnection yourself.
Remarks
ConnectionManager centralizes lifecycle concerns: it authenticates (login/registration/refresh), subscribes to token rotation, attempts to fetch and apply the E2E encryption key, constructs and registers handlers on the EchoHubConnection, and ensures channel membership state is tracked. It forwards the hub's runtime events (for example MessageReceived, UserJoined, ChannelUpdated) so callers receive high-level notifications and do not need to bind SignalR handlers directly. The class is intended as the single place that composes ApiClient, ClientEncryptionService/RoomKeyStore, and EchoHubConnection into a usable connection for the UI.
Notes
ConnectAsyncreports progress via theonStatuscallback and will throw on authentication failure — callers are expected to handle saved-session expiry and similar error flows.- Event handlers (for example
MessageReceived,UserJoined,ConnectionStatusChanged) may be invoked from signalr/connection threads; subscribers should not assume they run on the UI thread and must marshal to the UI thread when necessary. - Always
awaitdisposing the manager (it implementsIAsyncDisposable) so underlying resources such as theEchoHubConnectionandApiClientare cleanly released; failing to do so can leave connections or background work active.
ConnectResult
File:
src/EchoHub.Client/Services/ConnectionManager.cs
Kind: record
internal record ConnectResult(
LoginResponse Login,
List<ChannelDto> Channels,
Dictionary<string, List<MessageDto>> Histories)
Parameters:
| Parameter | Type | Default |
|---|---|---|
Login |
LoginResponse |
— |
Channels |
List<ChannelDto> |
— |
Histories |
Dictionary<string, List<MessageDto>> |
— |
ConnectResult represents the payload returned after a successful connection, carrying everything the AppOrchestrator needs to update the UI. It includes the authenticated login information (Login), the collection of available channels (Channels), and the initial per-channel histories (Histories), where each channel name maps to its starting list of messages, always including the default channel.
Remarks
ConnectResult is a record, so it participates in value-based equality and can be treated as a single unit when comparing connection outcomes. Note that its Channels and Histories collections are mutable (List<ChannelDto> and Dictionary<string, List<MessageDto>>); if you need true immutability, expose read-only wrappers or clone the collections when passing them onward.
Notes
- The contained
List<ChannelDto>andDictionary<string, List<MessageDto>>are mutable; avoid mutating them in place and consider treating theConnectResultas a snapshot that should be cloned if you require immutability downstream.