diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs index 4eccd35..df76440 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs @@ -5,13 +5,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using Newtonsoft.Json.Linq; 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; @@ -21,11 +15,14 @@ public class CanvasService { private Canvas2DContext? _currentCanvasContext; private IJSRuntime _jsRuntime; private IConnectionMultiplexer _cache; + private ChannelMessageQueue _channel; private CanvasCommand _lastCommand = new(); + private bool _is_started = false; public CanvasService(IJSRuntime jsRuntime, IConnectionMultiplexer cache) { _jsRuntime = jsRuntime; _cache = cache; + _channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal("lol")); } public string currentColor { get; set; } = "Black"; @@ -34,7 +31,16 @@ public class CanvasService { public ElementReference divCanvas { get; set; } public BECanvasComponent myCanvas { get; set; } = new(); - // TODO: Dessiner à partir des commandes de Franz. + public async void Consume() { + CancellationToken cToken = new(); + + while (!cToken.IsCancellationRequested) { + var mess = await _channel.ReadAsync(cToken); + var comm = JsonConvert.DeserializeObject(mess.Message); + if (comm is not null) + Draw(comm); + } + } public async void Draw(IEnumerable lsCommand) { if (_currentCanvasContext is null) { @@ -61,6 +67,11 @@ public class CanvasService { public async void HandleMouse(MouseEventArgs eventArgs) { double mouseX = 0, mouseY = 0; + if (!_is_started) { + Consume(); + _is_started = true; + } + 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. @@ -83,7 +94,7 @@ public class CanvasService { mouseX -= mouseX % pointSize; mouseY -= mouseY % pointSize; } - + command.X = mouseX; command.Y = mouseY; command.Color = color; @@ -91,10 +102,11 @@ public class CanvasService { if (command.Equals(_lastCommand)) return; // Pour pas spammer des commandes si c'est pas pertinent. - - // TODO: Shipper les commandes à Franz. - Draw(command); + // TODO: Shipper les commandes à Franz. + _cache.GetSubscriber().Publish(RedisChannel.Literal("lol"), JsonConvert.SerializeObject(command)); + + //Draw(command); _lastCommand = command; } diff --git a/BlazorCanvas/BlazorCanvas.Server/Program.cs b/BlazorCanvas/BlazorCanvas.Server/Program.cs index 0449ac2..a3cb9d7 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Program.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Program.cs @@ -10,7 +10,7 @@ builder.AddServiceDefaults(); // Add services to the container. builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); -builder.AddRedisOutputCache("cache"); +builder.AddRedis("cache"); builder.Services.AddScoped();