-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add blazor server demo project, fix an unhandled NRE when a hub tries…
… to send to a null userID or groupID
- Loading branch information
Showing
31 changed files
with
825 additions
and
11 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
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 @@ | ||
<Router AppAssembly="@typeof(App).Assembly"> | ||
<Found Context="routeData"> | ||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> | ||
<FocusOnNavigate RouteData="@routeData" Selector="h1" /> | ||
</Found> | ||
<NotFound> | ||
<PageTitle>Not found</PageTitle> | ||
<LayoutView Layout="@typeof(MainLayout)"> | ||
<p role="alert">Sorry, there's nothing at this address.</p> | ||
</LayoutView> | ||
</NotFound> | ||
</Router> |
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,84 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class UserIdProvider : IUserIdProvider | ||
{ | ||
public virtual string GetUserId(HubConnectionContext connection) | ||
{ | ||
return "staticUserid"; | ||
} | ||
} | ||
|
||
public class ChatHubA : Hub | ||
{ | ||
private readonly ILogger<ChatHubA> _logger; | ||
|
||
public ChatHubA(ILogger<ChatHubA> logger) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
private readonly string groupId = Guid.Empty.ToString(); | ||
|
||
public async Task SendMessage(string name, string message) | ||
{ | ||
// _logger.LogInformation($"{nameof(SendMessage)} called. ConnectionId:{Context.ConnectionId}, Name:{name}, Message:{message}"); | ||
await Clients.Group(groupId).SendAsync("BroadcastMessage", name, message); | ||
} | ||
|
||
public override async Task OnConnectedAsync() | ||
{ | ||
_logger.LogInformation($"{nameof(OnConnectedAsync)} called."); | ||
|
||
await base.OnConnectedAsync(); | ||
await Groups.AddToGroupAsync(Context.ConnectionId, Guid.Empty.ToString()); | ||
} | ||
|
||
public override async Task OnDisconnectedAsync(Exception exception) | ||
{ | ||
_logger.LogInformation(exception, $"{nameof(OnDisconnectedAsync)} called."); | ||
|
||
await base.OnDisconnectedAsync(exception); | ||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, Guid.Empty.ToString()); | ||
} | ||
} | ||
|
||
public class ChatHubB : Hub | ||
{ | ||
private readonly ILogger<ChatHubB> _logger; | ||
|
||
public ChatHubB(ILogger<ChatHubB> logger) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
} | ||
|
||
private readonly string groupId = Guid.Empty.ToString(); | ||
|
||
public async Task SendMessage(string name, string message) | ||
{ | ||
// _logger.LogInformation($"{nameof(SendMessage)} called. ConnectionId:{Context.ConnectionId}, Name:{name}, Message:{message}"); | ||
await Clients.Group(groupId).SendAsync("BroadcastMessage", name, "group"); | ||
await Clients.Caller.SendAsync("BroadcastMessage", name, "caller"); | ||
await Clients.User(Context.UserIdentifier).SendAsync("BroadcastMessage", name, "user"); | ||
await Clients.All.SendAsync("BroadcastMessage", name, "all"); | ||
await Clients.AllExcept(Context.ConnectionId).SendAsync("BroadcastMessage", name, "allExcept"); | ||
} | ||
|
||
public override async Task OnConnectedAsync() | ||
{ | ||
_logger.LogInformation($"{nameof(OnConnectedAsync)} called."); | ||
|
||
await base.OnConnectedAsync(); | ||
await Groups.AddToGroupAsync(Context.ConnectionId, Guid.Empty.ToString()); | ||
} | ||
|
||
public override async Task OnDisconnectedAsync(Exception exception) | ||
{ | ||
_logger.LogInformation(exception, $"{nameof(OnDisconnectedAsync)} called."); | ||
|
||
await base.OnDisconnectedAsync(exception); | ||
await Groups.RemoveFromGroupAsync(Context.ConnectionId, Guid.Empty.ToString()); | ||
} | ||
} |
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,13 @@ | ||
namespace DemoBlazorServer.Data | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} | ||
} |
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,20 @@ | ||
namespace DemoBlazorServer.Data | ||
{ | ||
public class WeatherForecastService | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate) | ||
{ | ||
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = startDate.AddDays(index), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}).ToArray()); | ||
} | ||
} | ||
} |
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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\IntelliTect.AspNetCore.SignalR.SqlServer\IntelliTect.AspNetCore.SignalR.SqlServer.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Update="wwwroot\css\site.css"> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
</Project> |
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,77 @@ | ||
@using Microsoft.AspNetCore.SignalR.Client | ||
@inject NavigationManager Navigation | ||
@implements IAsyncDisposable | ||
|
||
<h1>Hub @Suffix</h1> | ||
|
||
<div class="container"> | ||
<div class="row"> </div> | ||
<div class="row"> | ||
<div class="col-2">User</div> | ||
<div class="col-4"><input type="text" @bind="userInput" /></div> | ||
</div> | ||
<div class="row"> | ||
<div class="col-2">Message</div> | ||
<div class="col-4"><input type="text" @bind="messageInput" /></div> | ||
</div> | ||
<div class="row"> </div> | ||
<div class="row"> | ||
<div class="col-6"> | ||
<input type="button" @onclick="Send" disabled="@(!IsConnected)" value="Send Message" /> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<hr> | ||
|
||
<ul id="messagesList"> | ||
@foreach (var message in messages) | ||
{ | ||
<li>@message</li> | ||
} | ||
</ul> | ||
|
||
@code { | ||
[Parameter] | ||
public string Suffix { get; set; } | ||
|
||
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" + Suffix)) | ||
.Build(); | ||
|
||
hubConnection.On<string, string>("BroadcastMessage", (user, message) => | ||
{ | ||
var encodedMsg = $"{user}: {message}"; | ||
messages.Add(encodedMsg); | ||
InvokeAsync(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
@page "/counter" | ||
|
||
<PageTitle>Counter</PageTitle> | ||
|
||
<h1>Counter</h1> | ||
|
||
<p role="status">Current count: @currentCount</p> | ||
|
||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> | ||
|
||
@code { | ||
private int currentCount = 0; | ||
|
||
private void IncrementCount() | ||
{ | ||
currentCount++; | ||
} | ||
} |
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,42 @@ | ||
@page | ||
@model DemoBlazorServer.Pages.ErrorModel | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> | ||
<title>Error</title> | ||
<link href="~/css/bootstrap/bootstrap.min.css" rel="stylesheet" /> | ||
<link href="~/css/site.css" rel="stylesheet" asp-append-version="true" /> | ||
</head> | ||
|
||
<body> | ||
<div class="main"> | ||
<div class="content px-4"> | ||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (Model.ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@Model.RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> | ||
</div> | ||
</div> | ||
</body> | ||
|
||
</html> |
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,27 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using System.Diagnostics; | ||
|
||
namespace DemoBlazorServer.Pages | ||
{ | ||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] | ||
[IgnoreAntiforgeryToken] | ||
public class ErrorModel : PageModel | ||
{ | ||
public string? RequestId { get; set; } | ||
|
||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
|
||
private readonly ILogger<ErrorModel> _logger; | ||
|
||
public ErrorModel(ILogger<ErrorModel> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public void OnGet() | ||
{ | ||
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; | ||
} | ||
} | ||
} |
Oops, something went wrong.