From 2bea742a6959684139cc6bfe085fe4bc280a4bcc Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Sat, 18 Nov 2023 12:19:37 -0500 Subject: [PATCH] Ajouts du mode stream + ajouts QoL. --- .../Components/Data/CanvasService.cs | 46 ++++++++++++++++--- .../Components/Data/IRedisService.cs | 10 +++- .../Components/Data/RedisService.cs | 6 +-- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs index c7893c0..ad1c91d 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/CanvasService.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using Newtonsoft.Json.Linq; using Newtonsoft.Json; +using System.ComponentModel; namespace BlazorCanvas.Server.Components.Data; @@ -15,13 +16,21 @@ public class CanvasService { private IJSRuntime _jsRuntime; private IRedisService _redisService; private CanvasCommand _lastCommand = new(); - private bool _is_started = false; + private List _lsComms = new(); + private bool _is_init = false, + _is_started = false, + _has_ended = false; public CanvasService(IJSRuntime jsRuntime, IRedisService redisService) { _jsRuntime = jsRuntime; _redisService = redisService; } + ~CanvasService() { + _has_ended = true; + Task.Delay(100); + } + public string currentColor { get; set; } = "Black"; public int pointSize { get; set; } = 1; public bool snap { get; set; } @@ -40,6 +49,26 @@ public class CanvasService { } } + public async void Consume() { + while (!_has_ended) { + if (_lsComms.Count > 0) + _lsComms.Clear(); + _lsComms.AddRange(await _redisService.Consume()); + if (_lsComms.Count == 1) + Draw(_lsComms[0]); + else if (_lsComms.Count > 0) + Draw(_lsComms); + } + } + + public async void Init() { + while (!_is_init) { + _is_init = await _redisService.InitStreamer(); + if (!_is_init) + await Task.Delay(1000); + } + } + public async void Draw(IEnumerable lsCommand) { if (_currentCanvasContext is null) { _currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); @@ -70,8 +99,11 @@ public class CanvasService { public async void HandleMouse(MouseEventArgs eventArgs) { double mouseX = 0, mouseY = 0; - if (!_is_started) { - Subscribe(); + if (!_is_started) { + if (!_is_init) + Init(); + if (_is_init) + Consume(); _is_started = true; } @@ -105,10 +137,12 @@ public class CanvasService { if (command.Equals(_lastCommand)) return; // Pour pas spammer des commandes si c'est pas pertinent. - - _redisService.Publish(command); - //Draw(command); // Local + _redisService.Produce(command); // Stream + + // _redisService.Publish(command); // Pub/Sub + + // Draw(command); // Local _lastCommand = command; } diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/IRedisService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/IRedisService.cs index 3431d6b..36aa4a0 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/IRedisService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/IRedisService.cs @@ -1,6 +1,12 @@ namespace BlazorCanvas.Server.Components.Data; public interface IRedisService { - public Task Subscribe(CancellationToken cToken); - public void Publish(CanvasCommand command); + Task InitStreamer(); + bool InitSubscriber(); + + Task Subscribe(CancellationToken cToken); + void Publish(CanvasCommand command); + + void Produce(CanvasCommand command); + Task> Consume(); } diff --git a/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs b/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs index 2af5e6f..dd19c60 100644 --- a/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs +++ b/BlazorCanvas/BlazorCanvas.Server/Components/Data/RedisService.cs @@ -13,7 +13,7 @@ public class RedisService : IRedisService { GROUP_NAME = "groupie", SUB_NAME = "servant"; - private NameValueEntry[] arnve = new NameValueEntry[1]; + private NameValueEntry[] arNve = new NameValueEntry[1]; private IConnectionMultiplexer _cache; private IDatabase _database; @@ -55,8 +55,8 @@ public class RedisService : IRedisService { } public async void Produce(CanvasCommand command) { - arnve[0] = new NameValueEntry("command", JsonConvert.SerializeObject(command)); - await _database.StreamAddAsync(STREAM_NAME, arnve); + arNve[0] = new NameValueEntry("command", JsonConvert.SerializeObject(command)); + await _database.StreamAddAsync(STREAM_NAME, arNve); } public async Task> Consume() {