Skip to content

Commit cbfed00

Browse files
authored
SignalR Hubs v6.0 (dotnet#3)
1 parent 7e5a495 commit cbfed00

20 files changed

+403
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
##
44
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
55

6+
Properties/
7+
*.sln
8+
69
# User-specific files
710
*.rsuser
811
*.suo
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace SignalRHubsSample.Hubs;
4+
5+
// <snippet_Class>
6+
public class ChatHub : Hub
7+
{
8+
public async Task SendMessage(string user, string message)
9+
=> await Clients.All.SendAsync("ReceiveMessage", user, message);
10+
}
11+
// </snippet_Class>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@page
2+
@model ErrorModel
3+
@{
4+
ViewData["Title"] = "Error";
5+
}
6+
7+
<h1 class="text-danger">Error.</h1>
8+
<h2 class="text-danger">An error occurred while processing your request.</h2>
9+
10+
@if (Model.ShowRequestId)
11+
{
12+
<p>
13+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
14+
</p>
15+
}
16+
17+
<h3>Development Mode</h3>
18+
<p>
19+
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
20+
</p>
21+
<p>
22+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
23+
It can result in displaying sensitive information from exceptions to end users.
24+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
25+
and restarting the app.
26+
</p>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Diagnostics;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Mvc.RazorPages;
4+
5+
namespace SignalRHubsSample.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+
}
27+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@page
2+
@model IndexModel
3+
4+
@{
5+
ViewData["Title"] = "Home";
6+
}
7+
8+
<div class="form-group row">
9+
<label for="userInput" class="col-2 col-form-label">User</label>
10+
<div class="col col-sm-6">
11+
<input id="userInput" class="form-control" />
12+
</div>
13+
</div>
14+
15+
<div class="form-group row">
16+
<label for="message" class="col-2 col-form-label">Message</label>
17+
<div class="col col-sm-6">
18+
<input id="messageInput" class="form-control" />
19+
</div>
20+
</div>
21+
22+
<button id="send" type="button" class="btn btn-primary">Send Message</button>
23+
24+
<hr>
25+
26+
<ul id="messageList"></ul>
27+
28+
@section Scripts
29+
{
30+
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.js"></script>
31+
<script src="~/chat.js"></script>
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.RazorPages;
3+
4+
namespace SignalRHubsSample.Pages;
5+
6+
public class IndexModel : PageModel
7+
{
8+
private readonly ILogger<IndexModel> _logger;
9+
10+
public IndexModel(ILogger<IndexModel> logger)
11+
{
12+
_logger = logger;
13+
}
14+
15+
public void OnGet()
16+
{
17+
18+
}
19+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - SignalRHubsSample</title>
7+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" />
8+
<link rel="stylesheet" href="~/site.css" asp-append-version="true" />
9+
<link rel="stylesheet" href="~/SignalRHubsSample.styles.css" asp-append-version="true" />
10+
</head>
11+
<body>
12+
<header>
13+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
14+
<div class="container">
15+
<a class="navbar-brand" asp-area="" asp-page="/Index">SignalRHubsSample</a>
16+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
17+
aria-expanded="false" aria-label="Toggle navigation">
18+
<span class="navbar-toggler-icon"></span>
19+
</button>
20+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
21+
<ul class="navbar-nav flex-grow-1">
22+
<li class="nav-item">
23+
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
24+
</li>
25+
</ul>
26+
</div>
27+
</div>
28+
</nav>
29+
</header>
30+
<div class="container">
31+
<main role="main" class="pb-3">
32+
@RenderBody()
33+
</main>
34+
</div>
35+
36+
<footer class="border-top footer text-muted">
37+
<div class="container">
38+
&copy; 2022 - SignalRHubsSample
39+
</div>
40+
</footer>
41+
42+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
43+
44+
@await RenderSectionAsync("Scripts", required: false)
45+
</body>
46+
</html>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
2+
for details on configuring this project to bundle and minify static web assets. */
3+
4+
a.navbar-brand {
5+
white-space: normal;
6+
text-align: center;
7+
word-break: break-all;
8+
}
9+
10+
a {
11+
color: #0077cc;
12+
}
13+
14+
.btn-primary {
15+
color: #fff;
16+
background-color: #1b6ec2;
17+
border-color: #1861ac;
18+
}
19+
20+
.nav-pills .nav-link.active, .nav-pills .show > .nav-link {
21+
color: #fff;
22+
background-color: #1b6ec2;
23+
border-color: #1861ac;
24+
}
25+
26+
.border-top {
27+
border-top: 1px solid #e5e5e5;
28+
}
29+
.border-bottom {
30+
border-bottom: 1px solid #e5e5e5;
31+
}
32+
33+
.box-shadow {
34+
box-shadow: 0 .25rem .75rem rgba(0, 0, 0, .05);
35+
}
36+
37+
button.accept-policy {
38+
font-size: 1rem;
39+
line-height: inherit;
40+
}
41+
42+
.footer {
43+
position: absolute;
44+
bottom: 0;
45+
width: 100%;
46+
white-space: nowrap;
47+
line-height: 60px;
48+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@using SignalRHubsSample
2+
@namespace SignalRHubsSample.Pages
3+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
Layout = "_Layout";
3+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using SignalRHubsSample.Hubs;
2+
3+
// <snippet_AddSignalR>
4+
var builder = WebApplication.CreateBuilder(args);
5+
6+
builder.Services.AddRazorPages();
7+
builder.Services.AddSignalR();
8+
// </snippet_AddSignalR>
9+
10+
var app = builder.Build();
11+
12+
if (!app.Environment.IsDevelopment())
13+
{
14+
app.UseExceptionHandler("/Error");
15+
app.UseHsts();
16+
}
17+
18+
app.UseHttpsRedirection();
19+
app.UseStaticFiles();
20+
21+
app.UseRouting();
22+
23+
app.UseAuthorization();
24+
25+
// <snippet_MapHub>
26+
app.MapRazorPages();
27+
app.MapHub<ChatHub>("/Chat");
28+
29+
app.Run();
30+
// </snippet_MapHub>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace SignalRHubsSample.Snippets.Hubs;
4+
5+
public class ChatHub : Hub
6+
{
7+
// <snippet_OnConnectedAsync>
8+
public override async Task OnConnectedAsync()
9+
{
10+
await Groups.AddToGroupAsync(Context.ConnectionId, "SignalR Users");
11+
await base.OnConnectedAsync();
12+
}
13+
// </snippet_OnConnectedAsync>
14+
15+
// <snippet_OnDisconnectedAsync>
16+
public override async Task OnDisconnectedAsync(Exception? exception)
17+
{
18+
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users");
19+
await base.OnDisconnectedAsync(exception);
20+
}
21+
// </snippet_OnDisconnectedAsync>
22+
23+
// <snippet_Clients>
24+
public async Task SendMessage(string user, string message)
25+
=> await Clients.All.SendAsync("ReceiveMessage", user, message);
26+
27+
public async Task SendMessageToCaller(string user, string message)
28+
=> await Clients.Caller.SendAsync("ReceiveMessage", user, message);
29+
30+
public async Task SendMessageToGroup(string user, string message)
31+
=> await Clients.Group("SignalR Users").SendAsync("ReceiveMessage", user, message);
32+
// </snippet_Clients>
33+
34+
// <snippet_HubMethodName>
35+
[HubMethodName("SendMessageToUser")]
36+
public async Task DirectMessage(string user, string message)
37+
=> await Clients.User(user).SendAsync("ReceiveMessage", user, message);
38+
// </snippet_HubMethodName>
39+
40+
// <snippet_ThrowException>
41+
public Task ThrowException()
42+
=> throw new HubException("This error will be sent to the client!");
43+
// </snippet_ThrowException>
44+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SignalRHubsSample.Snippets.Hubs;
2+
3+
// <snippet_Interface>
4+
public interface IChatClient
5+
{
6+
Task ReceiveMessage(string user, string message);
7+
}
8+
// </snippet_Interface>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace SignalRHubsSample.Snippets.Hubs;
4+
5+
// <snippet_Class>
6+
public class StronglyTypedChatHub : Hub<IChatClient>
7+
{
8+
public async Task SendMessage(string user, string message)
9+
=> await Clients.All.ReceiveMessage(user, message);
10+
11+
public async Task SendMessageToCaller(string user, string message)
12+
=> await Clients.Caller.ReceiveMessage(user, message);
13+
14+
public async Task SendMessageToGroup(string user, string message)
15+
=> await Clients.Group("SignalR Users").ReceiveMessage(user, message);
16+
}
17+
// </snippet_Class>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"DetailedErrors": true,
3+
"Logging": {
4+
"LogLevel": {
5+
"Default": "Information",
6+
"Microsoft.AspNetCore": "Warning"
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)