1
0

Redis fonctionnel ... mais on ne peut pas avoir les dessins passés quand on ouvre. (Pas de persistence)

This commit is contained in:
MarcEricMartel 2023-11-15 15:50:18 -05:00
parent 336333132c
commit 517b6870fc
2 changed files with 24 additions and 12 deletions

View File

@ -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<CanvasCommand>(mess.Message);
if (comm is not null)
Draw(comm);
}
}
public async void Draw(IEnumerable<CanvasCommand> 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;
}

View File

@ -10,7 +10,7 @@ builder.AddServiceDefaults();
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.AddRedisOutputCache("cache");
builder.AddRedis("cache");
builder.Services.AddScoped<CanvasService>();