From 1d245662b6688d995d6ada847beba1a34e5d85df Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Wed, 15 Nov 2023 12:32:30 -0500 Subject: [PATCH] REDIS --- .../BlazorCanvas.Server.csproj | 1 + .../Components/Data/CanvasService.cs | 43 ++++++++----------- BlazorCanvas/BlazorCanvas.Server/Program.cs | 3 ++ 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj b/BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj index 5a72f6b..3d24c4c 100644 --- a/BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj +++ b/BlazorCanvas/BlazorCanvas.Server/BlazorCanvas.Server.csproj @@ -7,6 +7,7 @@ + diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs index 939629c..4eccd35 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs @@ -8,20 +8,24 @@ using Newtonsoft.Json; using System.Drawing; using System.Threading; using Microsoft.AspNetCore.Mvc.ApplicationModels; +using Microsoft.AspNetCore.Connections; +using Aspire.StackExchange.Redis; +using StackExchange.Redis; +using System.Runtime.CompilerServices; namespace BlazorCanvas.Server.Components.Data; // https://www.codeproject.com/Articles/5269947/Drawing-with-the-HTML-Canvas-Element-in-Blazor-Ser -public class CanvasService -{ +public class CanvasService { private Canvas2DContext? _currentCanvasContext; private IJSRuntime _jsRuntime; + private IConnectionMultiplexer _cache; private CanvasCommand _lastCommand = new(); - public CanvasService(IJSRuntime jsRuntime) - { + public CanvasService(IJSRuntime jsRuntime, IConnectionMultiplexer cache) { _jsRuntime = jsRuntime; + _cache = cache; } public string currentColor { get; set; } = "Black"; @@ -32,26 +36,21 @@ public class CanvasService // TODO: Dessiner à partir des commandes de Franz. - public async void Draw(IEnumerable lscommand) - { - if (_currentCanvasContext is null) - { + public async void Draw(IEnumerable lsCommand) { + if (_currentCanvasContext is null) { _currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080); } await _currentCanvasContext.BeginBatchAsync(); - foreach (CanvasCommand command in lscommand) - { + foreach (CanvasCommand command in lsCommand) { await _currentCanvasContext.SetFillStyleAsync(command.Color); await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize); } await _currentCanvasContext.EndBatchAsync(); } - public async void Draw(CanvasCommand command) - { - if (_currentCanvasContext is null) - { + public async void Draw(CanvasCommand command) { + if (_currentCanvasContext is null) { _currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080); } @@ -59,23 +58,20 @@ public class CanvasService await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize); } - public async void HandleMouse(MouseEventArgs eventArgs) - { + public async void HandleMouse(MouseEventArgs eventArgs) { double mouseX = 0, mouseY = 0; if (eventArgs.Buttons == 0 || eventArgs.Buttons > 2) return; // Rien faire si aucun bouton est appuyé ou si les deux boutons/ d'autres boutons sont appuyés. - if (divCanvas.Id?.Length > 0) - { + if (divCanvas.Id?.Length > 0) { string data = await _jsRuntime.InvokeAsync("getDivCanvasOffsets", new object[] { divCanvas }); string color = "White"; CanvasCommand command = new(); JObject? offsets = (JObject?)JsonConvert.DeserializeObject(data); - if (offsets is not null && offsets.HasValues) - { // Translation entre le canvas et la souris. + if (offsets is not null && offsets.HasValues) { // Translation entre le canvas et la souris. mouseX = eventArgs.PageX - offsets.Value("offsetLeft"); mouseY = eventArgs.PageY - offsets.Value("offsetTop"); } @@ -83,12 +79,11 @@ public class CanvasService if (eventArgs.Buttons == 1) // Couleur si bouton gauche, blanc si bouton droit color = currentColor; - if (snap) - { // Magnétisme boboche. + if (snap) { // Magnétisme boboche. mouseX -= mouseX % pointSize; mouseY -= mouseY % pointSize; } - + command.X = mouseX; command.Y = mouseY; command.Color = color; @@ -98,7 +93,7 @@ public class CanvasService return; // Pour pas spammer des commandes si c'est pas pertinent. // TODO: Shipper les commandes à Franz. - + Draw(command); _lastCommand = command; diff --git a/BlazorCanvas/BlazorCanvas.Server/Program.cs b/BlazorCanvas/BlazorCanvas.Server/Program.cs index 103fd8c..cf8dd60 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Program.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Program.cs @@ -1,5 +1,6 @@ using BlazorCanvas.Server.Components; using BlazorCanvas.Server.Components.Data; +using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Hosting; var builder = WebApplication.CreateBuilder(args); @@ -9,6 +10,8 @@ builder.AddServiceDefaults(); // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); +builder.AddRedisOutputCache("cache"); +//builder.AddKeyedRabbitMQ("mq"); builder.Services.AddScoped();