Skip to content

Commit

Permalink
Signalr as Per Microsoft Learn Tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLParker committed May 16, 2023
1 parent 31f3f57 commit 70c26e2
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
68 changes: 65 additions & 3 deletions SignalRServerMessages/Client/Pages/Index.razor
@@ -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();
}
}
}
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.5" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions SignalRServerMessages/Server/Hubs/ChatHub.cs
@@ -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);
}
}
}
10 changes: 10 additions & 0 deletions SignalRServerMessages/Server/Program.cs
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.ResponseCompression;
using SignalRServerMessages.Server.Hubs;

namespace SignalRServerMessages
{
Expand All @@ -9,13 +10,21 @@ public static void Main(string[] args)
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddSignalR();
builder.Services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseResponseCompression();

if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
Expand All @@ -38,6 +47,7 @@ public static void Main(string[] args)
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.MapHub<ChatHub>("/chathub");

app.Run();
}
Expand Down

0 comments on commit 70c26e2

Please sign in to comment.