Skip to content

Commit

Permalink
Add dummy SignalR chat.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Del Rincón López committed Oct 23, 2023
1 parent 1c86b9c commit 317c38a
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 3 deletions.
6 changes: 3 additions & 3 deletions BlazorApp1.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp1.Server.Integrati
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp1.Server.Unit.Tests", "BlazorApp1.Server.Unit.Tests\BlazorApp1.Server.Unit.Tests.csproj", "{C9524ABC-7B46-480C-B202-25004E87D082}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp1.Server.Abstractions", "BlazorApp1.Server.Abstractions\BlazorApp1.Server.Abstractions.csproj", "{78DBBB58-25F0-4B4D-884F-FA6DF377B0DB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp1.Server.Abstractions", "BlazorApp1.Server.Abstractions\BlazorApp1.Server.Abstractions.csproj", "{78DBBB58-25F0-4B4D-884F-FA6DF377B0DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp1.Application", "BlazorApp1.Application\BlazorApp1.Application.csproj", "{36FC5117-C425-4F3A-BEED-B8F76EE2D5B8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp1.Application", "BlazorApp1.Application\BlazorApp1.Application.csproj", "{36FC5117-C425-4F3A-BEED-B8F76EE2D5B8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorApp1.Application.Unit.Tests", "BlazorApp1.Application.Unit.Tests\BlazorApp1.Application.Unit.Tests.csproj", "{192F7AAD-7CDE-4C39-9409-83C822A28D03}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorApp1.Application.Unit.Tests", "BlazorApp1.Application.Unit.Tests\BlazorApp1.Application.Unit.Tests.csproj", "{192F7AAD-7CDE-4C39-9409-83C822A28D03}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
55 changes: 55 additions & 0 deletions BlazorApp1.sln.startup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
This is a configuration file for the SwitchStartupProject Visual Studio Extension
See https://heptapod.host/thirteen/switchstartupproject/blob/branch/current/Configuration.md
*/
{
/* Configuration File Version */
"Version": 3,

/* Create an item in the dropdown list for each project in the solution? */
"ListAllProjects": true,

/*
Dictionary of named configurations with one or multiple startup projects
and optional parameters like command line arguments and working directory.
Example:

"MultiProjectConfigurations": {
"A + B (Ext)": {
"Projects": {
"MyProjectA": {},
"MyProjectB": {
"CommandLineArguments": "1234",
"WorkingDirectory": "%USERPROFILE%\\test",
"StartExternalProgram": "c:\\myprogram.exe"
}
}
},
"A + B": {
"Projects": {
"MyProjectA": {},
"MyProjectB": {
"CommandLineArguments": "",
"WorkingDirectory": "",
"StartProject": true
}
}
},
"D (Debug x86)": {
"Projects": {
"MyProjectD": {}
},
"SolutionConfiguration": "Debug",
"SolutionPlatform": "x86",
},
"D (Release x64)": {
"Projects": {
"MyProjectD": {}
},
"SolutionConfiguration": "Release",
"SolutionPlatform": "x64",
}
}
*/
"MultiProjectConfigurations": {}
}
1 change: 1 addition & 0 deletions BlazorApp1/Client/BlazorApp1.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.11" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.12" />
</ItemGroup>

<ItemGroup>
Expand Down
71 changes: 71 additions & 0 deletions BlazorApp1/Client/Pages/Chat.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@page "/Chat"
@using Microsoft.AspNetCore.SignalR.Client
@inject NavigationManager Navigation
@implements IAsyncDisposable

<PageTitle>Chat</PageTitle>

<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>

<hr>

<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();
}
}
}
11 changes: 11 additions & 0 deletions BlazorApp1/Server/Hubs/Class.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.SignalR;

namespace BlazorApp1.Server.Hubs;

public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
14 changes: 14 additions & 0 deletions BlazorApp1/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using BlazorApp1.Data;
using BlazorApp1.Server.Abstractions.Contracts.JsonContext;
using BlazorApp1.Server.BackgroundServices;
using BlazorApp1.Server.Hubs;
using BlazorApp1.Server.Profiles;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.EntityFrameworkCore;
using System.Text.Encodings.Web;

Expand All @@ -18,6 +20,13 @@

builder.Services.AddRazorPages();

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

builder.Services.AddWeatherForecastDataLayer();
builder.Services.AddWeatherForecastApplicationLayer(builder.Configuration);

Expand All @@ -42,6 +51,8 @@
app.UseHsts();
}

app.UseResponseCompression();

app.UseHttpsRedirection();

app.UseBlazorFrameworkFiles();
Expand All @@ -52,6 +63,9 @@

app.MapRazorPages();
app.MapControllers();

app.MapHub<ChatHub>("/chathub");

app.MapFallbackToFile("index.html");

app.Run();
Expand Down

0 comments on commit 317c38a

Please sign in to comment.