Skip to content

Commit 4235698

Browse files
signalR
0 parents  commit 4235698

37 files changed

+1934
-0
lines changed

.gitignore

+454
Large diffs are not rendered by default.

App.razor

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Router AppAssembly="@typeof(App).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
5+
</Found>
6+
<NotFound>
7+
<PageTitle>Not found</PageTitle>
8+
<LayoutView Layout="@typeof(MainLayout)">
9+
<p role="alert">Sorry, there's nothing at this address.</p>
10+
</LayoutView>
11+
</NotFound>
12+
</Router>

Data/WeatherForecast.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace server.Data;
2+
3+
public class WeatherForecast
4+
{
5+
public DateTime Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}

Data/WeatherForecastService.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace server.Data;
2+
3+
public class WeatherForecastService
4+
{
5+
private static readonly string[] Summaries = new[]
6+
{
7+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
8+
};
9+
10+
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
11+
{
12+
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
13+
{
14+
Date = startDate.AddDays(index),
15+
TemperatureC = Random.Shared.Next(-20, 55),
16+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
17+
}).ToArray());
18+
}
19+
}

Hubs/ChatHub.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace server.Hubs;
4+
5+
public class ChatHub: Hub
6+
{
7+
public async Task SendMessage(string user, string message)
8+
{
9+
await Clients.All.SendAsync("ReceiveMessage", user, message);
10+
}
11+
}

Models/UserMessage.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
namespace server.Models;
3+
4+
public class UserMessage
5+
{
6+
public string Name { get; set; }
7+
8+
public string Message { get; set; }
9+
10+
public bool CurrentUser { get; set; }
11+
12+
public DateTime SentTime { get; set; }
13+
}

Pages/Counter.razor

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@page "/counter"
2+
3+
<PageTitle>Counter</PageTitle>
4+
5+
<h1>Counter</h1>
6+
7+
<p role="status">Current count: @currentCount</p>
8+
9+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
10+
11+
@code {
12+
private int currentCount = 0;
13+
14+
private void IncrementCount()
15+
{
16+
currentCount++;
17+
}
18+
}

Pages/Error.cshtml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
@page
2+
@model server.Pages.ErrorModel
3+
4+
<!DOCTYPE html>
5+
<html lang="en">
6+
7+
<head>
8+
<meta charset="utf-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
10+
<title>Error</title>
11+
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" />
12+
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" />
13+
</head>
14+
15+
<body>
16+
<div class="main">
17+
<div class="content px-4">
18+
<h1 class="text-danger">Error.</h1>
19+
<h2 class="text-danger">An error occurred while processing your request.</h2>
20+
21+
@if (Model.ShowRequestId)
22+
{
23+
<p>
24+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
25+
</p>
26+
}
27+
28+
<h3>Development Mode</h3>
29+
<p>
30+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
31+
</p>
32+
<p>
33+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
34+
It can result in displaying sensitive information from exceptions to end users.
35+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
36+
and restarting the app.
37+
</p>
38+
</div>
39+
</div>
40+
</body>
41+
42+
</html>

Pages/Error.cshtml.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Diagnostics;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
5+
namespace server.Pages;
6+
7+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
8+
[IgnoreAntiforgeryToken]
9+
public class ErrorModel : PageModel
10+
{
11+
public string RequestId { get; set; }
12+
13+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
14+
15+
private readonly ILogger<ErrorModel> _logger;
16+
17+
public ErrorModel(ILogger<ErrorModel> logger)
18+
{
19+
_logger = logger;
20+
}
21+
22+
public void OnGet()
23+
{
24+
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
25+
}
26+
}

Pages/FetchData.razor

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
@page "/fetchdata"
2+
3+
<PageTitle>Weather forecast</PageTitle>
4+
5+
@using server.Data
6+
@inject WeatherForecastService ForecastService
7+
8+
<h1>Weather forecast</h1>
9+
10+
<p>This component demonstrates fetching data from a service.</p>
11+
12+
@if (forecasts == null)
13+
{
14+
<p><em>Loading...</em></p>
15+
}
16+
else
17+
{
18+
<table class="table">
19+
<thead>
20+
<tr>
21+
<th>Date</th>
22+
<th>Temp. (C)</th>
23+
<th>Temp. (F)</th>
24+
<th>Summary</th>
25+
</tr>
26+
</thead>
27+
<tbody>
28+
@foreach (var forecast in forecasts)
29+
{
30+
<tr>
31+
<td>@forecast.Date.ToShortDateString()</td>
32+
<td>@forecast.TemperatureC</td>
33+
<td>@forecast.TemperatureF</td>
34+
<td>@forecast.Summary</td>
35+
</tr>
36+
}
37+
</tbody>
38+
</table>
39+
}
40+
41+
@code {
42+
private WeatherForecast[] forecasts;
43+
44+
protected override async Task OnInitializedAsync()
45+
{
46+
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
47+
}
48+
}

Pages/Index.razor

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
@page "/"
2+
@using Microsoft.AspNetCore.SignalR.Client
3+
@using Models
4+
@inject NavigationManager NavigationManager
5+
@implements IAsyncDisposable
6+
7+
<div class="container overflow-auto shadow-sm p-3 mb-5 bg-white rounded" style="height: 500px;">
8+
@if (!userMessages.Any())
9+
{
10+
<p>No messages yet, start chatting!</p>
11+
}
12+
13+
@foreach (var userMessage in userMessages)
14+
{
15+
<div class="row mb-3 d-flex @(userMessage.CurrentUser ? "justify-content-end" : "")">
16+
<div class="card shadow @(userMessage.CurrentUser ? "color-green mr-5" : "ml-5")" style="width: 18rem;">
17+
<div class="card-header">
18+
@(userMessage.CurrentUser ? "You" : userMessage.Name)
19+
</div>
20+
<ul class="list-group list-group-flush">
21+
<li class="list-group-item @(userMessage.CurrentUser ? "color-green" : "")">@userMessage.Message</li>
22+
</ul>
23+
<div class="card-footer">
24+
<span class="small">@userMessage.SentTime.ToString("HH:mm | MMM dd")</span>
25+
</div>
26+
</div>
27+
</div>
28+
}
29+
</div>
30+
31+
<div class="container">
32+
<div class="row">
33+
<div class="col-3">
34+
<input @bind="usernameInput" type="text" class="form-control" placeholder="Your name" readonly="@isUserReadonly"/>
35+
</div>
36+
<div class="col-6">
37+
<textarea @bind="messageInput" class="form-control" placeholder="Start typing..."></textarea>
38+
</div>
39+
<div class="col-3">
40+
<button type="button" @onclick="Send" disabled="@(!IsConnected)" class="btn btn-primary">Send</button>
41+
</div>
42+
</div>
43+
</div>
44+
45+
@code{
46+
private HubConnection hubConnection;
47+
private List<UserMessage> userMessages = new();
48+
private string usernameInput;
49+
private string messageInput;
50+
private bool isUserReadonly = false;
51+
52+
public bool IsConnected => hubConnection.State == HubConnectionState.Connected;
53+
54+
protected override async Task OnInitializedAsync()
55+
{
56+
hubConnection = new HubConnectionBuilder()
57+
.WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
58+
.Build();
59+
60+
hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
61+
{
62+
userMessages.Add(new UserMessage { Name = user, Message = message, CurrentUser = user == usernameInput, SentTime = DateTime.Now });
63+
64+
StateHasChanged();
65+
});
66+
67+
await hubConnection.StartAsync();
68+
}
69+
70+
private async Task Send()
71+
{
72+
if (!string.IsNullOrEmpty(usernameInput) && !string.IsNullOrEmpty(messageInput))
73+
{
74+
await hubConnection.SendAsync("SendMessage", usernameInput, messageInput);
75+
76+
isUserReadonly = true;
77+
messageInput = string.Empty;
78+
}
79+
}
80+
81+
public async ValueTask DisposeAsync()
82+
{
83+
if (hubConnection is not null)
84+
{
85+
await hubConnection.DisposeAsync();
86+
}
87+
}
88+
}

Pages/_Host.cshtml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/"
2+
@namespace server.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
@{
5+
Layout = "_Layout";
6+
}
7+
8+
<component type="typeof(App)" render-mode="ServerPrerendered" />

Pages/_Layout.cshtml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@using Microsoft.AspNetCore.Components.Web
2+
@namespace server.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
4+
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
<head>
8+
<meta charset="utf-8" />
9+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
10+
<base href="~/" />
11+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
12+
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
13+
<link href="css/site.css" rel="stylesheet" />
14+
<link href="server.styles.css" rel="stylesheet" />
15+
<component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
16+
</head>
17+
<body>
18+
@RenderBody()
19+
20+
<div id="blazor-error-ui">
21+
<environment include="Staging,Production">
22+
An error has occurred. This application may no longer respond until reloaded.
23+
</environment>
24+
<environment include="Development">
25+
An unhandled exception has occurred. See browser dev tools for details.
26+
</environment>
27+
<a href="" class="reload">Reload</a>
28+
<a class="dismiss">🗙</a>
29+
</div>
30+
31+
<script src="_framework/blazor.server.js"></script>
32+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
33+
</body>
34+
</html>

Program.cs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.AspNetCore.Components;
2+
using Microsoft.AspNetCore.Components.Web;
3+
using Microsoft.AspNetCore.ResponseCompression;
4+
using server.Data;
5+
using server.Hubs;
6+
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
// Add services to the container.
10+
builder.Services.AddRazorPages();
11+
builder.Services.AddServerSideBlazor();
12+
builder.Services.AddSingleton<WeatherForecastService>();
13+
builder.Services.AddResponseCompression(options => {
14+
options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] {"application/octet-stream"});
15+
});
16+
17+
var app = builder.Build();
18+
19+
// Configure the HTTP request pipeline.
20+
if (!app.Environment.IsDevelopment())
21+
{
22+
app.UseExceptionHandler("/Error");
23+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
24+
app.UseHsts();
25+
}
26+
27+
app.UseHttpsRedirection();
28+
29+
app.UseStaticFiles();
30+
31+
app.UseRouting();
32+
33+
app.MapBlazorHub();
34+
app.MapHub<ChatHub>("/chathub");
35+
app.MapFallbackToPage("/_Host");
36+
37+
app.Run();

0 commit comments

Comments
 (0)