Skip to content

Commit

Permalink
Merge pull request #9 from dotnet/wadepickett/27796SignalRTokenCancel…
Browse files Browse the repository at this point in the history
…lationTry3

SignalR Hubs CancelationToken
  • Loading branch information
meslubi2021 authored Dec 19, 2024
2 parents 0780865 + 359c996 commit f5b95c4
Showing 1 changed file with 3 additions and 33 deletions.
36 changes: 3 additions & 33 deletions aspnetcore/signalr/hubs.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,45 +123,15 @@ In addition to making calls to clients, the server can request a result from a c

There are two ways to use the API on the server, the first is to call `Client(...)` or `Caller` on the `Clients` property in a Hub method:

```csharp
public class ChatHub : Hub
{
public async Task<string> WaitForMessage(string connectionId)
{
var message = await Clients.Client(connectionId).InvokeAsync<string>(
"GetMessage");
return message;
}
}
```
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/signalr/hubs/samples/7.x/SignalRHubsSample/Snippets/Hubs/ChatHubClientResults.cs" id="snippet_HubClientReturn" highlight="8-10":::

The second way is to call `Client(...)` on an instance of [`IHubContext<T>`](xref:signalr/hubcontext):

```csharp
async Task SomeMethod(IHubContext<MyHub> context)
{
string result = await context.Clients.Client(connectionID).InvokeAsync<string>(
"GetMessage");
}
```
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/signalr/hubs/samples/7.x/SignalRHubsSample/Snippets/Hubs/ChatHubClientResultsContext.cs" id="snippet_HubClientReturnContext":::

Strongly-typed hubs can also return values from interface methods:

```csharp
public interface IClient
{
Task<string> GetMessage();
}

public class ChatHub : Hub<IClient>
{
public async Task<string> WaitForMessage(string connectionId)
{
string message = await Clients.Client(connectionId).GetMessage();
return message;
}
}
```
:::code language="csharp" source="~/../AspNetCore.Docs.Samples/signalr/hubs/samples/7.x/SignalRHubsSample/Snippets/Hubs/ChatHubClientResultsStronglyTyped.cs" id="snippet_HubClientResultsStronglyTyped":::

Clients return results in their `.On(...)` handlers, as shown below:

Expand Down

0 comments on commit f5b95c4

Please sign in to comment.