Custom Transports
Every transport, built in or yours, is a class, and its type is its identity. host.Stop<BleHostTransport>(),
HubConnectOptions.Prefer<HttpClientTransport>(), host.Endpoint<BleHubEndpoint>() and connection.Transport is NearbyHostTransport all go by type. A transport of your own plugs into the same places.
| Built in | Host | Client | Client endpoint |
|---|---|---|---|
| Bluetooth LE | BleHostTransport |
BleClientTransport |
BleHubEndpoint |
| HTTP | HttpHostTransport |
HttpClientTransport |
HttpHubEndpoint |
| Multipeer | MultipeerHostTransport |
MultipeerClientTransport |
|
| Nearby | NearbyHostTransport |
NearbyClientTransport |
Host: IHubTransport
Section titled “Host: IHubTransport”public sealed record LoraSettings(int Channel);
public sealed class LoraHostTransport(ILoraRadio radio) : IHubTransport{ public string DisplayName => "LoRa"; // logs, errors and UI - not its identity public bool IsSupported => radio.IsAvailable; // false: skipped quietly, so one registration works everywhere
public bool IsRunning(IHubBinding hub) => ...;
public Task Start(IReadOnlyList<IHubBinding> hubs, CancellationToken ct) { foreach (var hub in hubs) { var settings = hub.GetSettings<LoraSettings>(); // what UseTransport was given for this hub // listen on settings.Channel, and hand each client to hub.Endpoint } return Task.CompletedTask; }
public Task Stop(IReadOnlyList<IHubBinding> hubs, HubDisconnect disconnect) => ...;}
builder.Services.AddUniversalServer(server => server .AddHub<GameHub>("game", hub => hub .UseHttp("_mygame._tcp") .UseTransport<LoraHostTransport>(new LoraSettings(Channel: 7))));UseTransport<T>(settings)serves the hub overTand sets the transport up on the server. DI creates it once, however many hubs use it. The settings object comes back fromIHubBinding.GetSettings<T>(), by its type.server.AddTransport<T>()sets it up without a hub, for a server with only dynamic hubs. ImplementIDynamicHubTransport.CreateSettings(definition)to decide whether, and how, it serves each dynamic hub.- A transport that can’t start throws from
Start(PlatformNotSupportedExceptionwhere the platform doesn’t have it). The host skips it as long as another transport starts. - Renames: override
HostRenamedto re-advertise whenIUniversalHubHost.Renameis called.
Each IHubBinding has the hub’s Key and Name, its Transports, and its Endpoint, an IHubEndpoint. For each
client, the transport:
- Calls
Endpoint.Connect(connectionId, handshake, channel, ct)with the client’sHandshakeInfoand anIHubPeerChannelit implements. A returned reason means the hub refused the client; tell it and close. - Answers calls with
Endpoint.InvokeandEndpoint.Stream. Arguments and results are the encoded bytes, never re-serialized. AnInvokeresult withAbortRequestedmeans send the reply, thenEndpoint.Disconnect. - Reports a dropped client with
Endpoint.Disconnected(connectionId, channel, disconnect). - Optionally supports resume (
EnableResume,Resume,ResumeWindow), renames (Rename) and media channels (MediaChannelOpened).
The IHubPeerChannel is how the hub reaches the client: Push, Disconnect, HostRenamed, plus Transport (what
HubConnection.Transport reports) and GetFeature(type) (what connection.Get<T>() returns, for an extension like
connection.LoraNode()).
Client: IHubClientTransport
Section titled “Client: IHubClientTransport”public sealed record LoraHubEndpoint(int NodeId, int Rssi) : HubEndpoint;
public sealed class LoraClientTransport(ILoraRadio radio) : IHubClientTransport{ public string DisplayName => "LoRa"; public bool IsSupported => radio.IsAvailable;
// hosts of client.HubName as they come and go; a null endpoint means it is gone public IObservable<HubHostSighting> Discover(HubClientContext client) => ...;
public Task Connect(HubClientContext client, HubHostInfo host, CancellationToken ct) { var node = host.Endpoint<LoraHubEndpoint>()!; return client.Connect(events => new LoraConnection(radio, node, events), ct); }}
builder.Services.AddUniversalHubClient<IGameHub>("game", o => o .UseHttp("_mygame._tcp") .UseTransport(sp => new LoraClientTransport(sp.GetRequiredService<ILoraRadio>())));DiscoverreturnsHubHostSighting(name, hostId, endpoint)s. The client merges them with the other transports by name, andhost.Endpoint<LoraHubEndpoint>()returns yours.Connectgoes throughHubClientContext.Connect, handing it a factory for anIHubConnectionTransport: one connection’s link, which runs the handshake, carriesInvoke,Stream,Rename, files, media andClose, and reports pushes, renames, drops and resumes through theIHubConnectionTransportEventsit is given.client.ConnectOptionshas the handshake values andAutomaticReconnect.o.UseTransport(create, services)takes an optional registration callback for what the transport needs.

