Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signalr as Per Microsoft Learn Tutorial
- Loading branch information
1 parent
31f3f57
commit 70c26e2
Showing
4 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,71 @@ | ||
@page "/" | ||
@using Microsoft.AspNetCore.SignalR.Client | ||
@inject NavigationManager Navigation | ||
@implements IAsyncDisposable | ||
|
||
<PageTitle>Index</PageTitle> | ||
|
||
<h1>Hello, world!</h1> | ||
<div class="form-group"> | ||
<label> | ||
User: | ||
<input @bind="userInput" /> | ||
</label> | ||
</div> | ||
<div class="form-group"> | ||
<label> | ||
Message: | ||
<input @bind="messageInput" size="50" /> | ||
</label> | ||
</div> | ||
<button @onclick="Send" disabled="@(!IsConnected)">Send</button> | ||
|
||
Welcome to your new app. | ||
<hr> | ||
|
||
<SurveyPrompt Title="How is Blazor working for you?" /> | ||
<ul id="messagesList"> | ||
@foreach (var message in messages) | ||
{ | ||
<li>@message</li> | ||
} | ||
</ul> | ||
|
||
@code { | ||
private HubConnection? hubConnection; | ||
private List<string> messages = new List<string>(); | ||
private string? userInput; | ||
private string? messageInput; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
hubConnection = new HubConnectionBuilder() | ||
.WithUrl(Navigation.ToAbsoluteUri("/chathub")) | ||
.Build(); | ||
|
||
hubConnection.On<string, string>("ReceiveMessage", (user, message) => | ||
{ | ||
var encodedMsg = $"{user}: {message}"; | ||
messages.Add(encodedMsg); | ||
StateHasChanged(); | ||
}); | ||
|
||
await hubConnection.StartAsync(); | ||
} | ||
|
||
private async Task Send() | ||
{ | ||
if (hubConnection is not null) | ||
{ | ||
await hubConnection.SendAsync("SendMessage", userInput, messageInput); | ||
} | ||
} | ||
|
||
public bool IsConnected => | ||
hubConnection?.State == HubConnectionState.Connected; | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
if (hubConnection is not null) | ||
{ | ||
await hubConnection.DisposeAsync(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.AspNetCore.SignalR; | ||
|
||
namespace SignalRServerMessages.Server.Hubs | ||
{ | ||
public class ChatHub : Hub | ||
{ | ||
public async Task SendMessage(string user, string message) | ||
{ | ||
await Clients.All.SendAsync("ReceiveMessage", user, message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters