Redis fonctionnel ... mais on ne peut pas avoir les dessins passés quand on ouvre. (Pas de persistence)
This commit is contained in:
parent
336333132c
commit
517b6870fc
@ -5,13 +5,7 @@ using Microsoft.AspNetCore.Components;
|
|||||||
using Microsoft.JSInterop;
|
using Microsoft.JSInterop;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using Newtonsoft.Json;
|
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 StackExchange.Redis;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace BlazorCanvas.Server.Components.Data;
|
namespace BlazorCanvas.Server.Components.Data;
|
||||||
|
|
||||||
@ -21,11 +15,14 @@ public class CanvasService {
|
|||||||
private Canvas2DContext? _currentCanvasContext;
|
private Canvas2DContext? _currentCanvasContext;
|
||||||
private IJSRuntime _jsRuntime;
|
private IJSRuntime _jsRuntime;
|
||||||
private IConnectionMultiplexer _cache;
|
private IConnectionMultiplexer _cache;
|
||||||
|
private ChannelMessageQueue _channel;
|
||||||
private CanvasCommand _lastCommand = new();
|
private CanvasCommand _lastCommand = new();
|
||||||
|
private bool _is_started = false;
|
||||||
|
|
||||||
public CanvasService(IJSRuntime jsRuntime, IConnectionMultiplexer cache) {
|
public CanvasService(IJSRuntime jsRuntime, IConnectionMultiplexer cache) {
|
||||||
_jsRuntime = jsRuntime;
|
_jsRuntime = jsRuntime;
|
||||||
_cache = cache;
|
_cache = cache;
|
||||||
|
_channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal("lol"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public string currentColor { get; set; } = "Black";
|
public string currentColor { get; set; } = "Black";
|
||||||
@ -34,7 +31,16 @@ public class CanvasService {
|
|||||||
public ElementReference divCanvas { get; set; }
|
public ElementReference divCanvas { get; set; }
|
||||||
public BECanvasComponent myCanvas { get; set; } = new();
|
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) {
|
public async void Draw(IEnumerable<CanvasCommand> lsCommand) {
|
||||||
if (_currentCanvasContext is null) {
|
if (_currentCanvasContext is null) {
|
||||||
@ -61,6 +67,11 @@ public class CanvasService {
|
|||||||
public async void HandleMouse(MouseEventArgs eventArgs) {
|
public async void HandleMouse(MouseEventArgs eventArgs) {
|
||||||
double mouseX = 0, mouseY = 0;
|
double mouseX = 0, mouseY = 0;
|
||||||
|
|
||||||
|
if (!_is_started) {
|
||||||
|
Consume();
|
||||||
|
_is_started = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (eventArgs.Buttons == 0 || eventArgs.Buttons > 2)
|
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.
|
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;
|
mouseX -= mouseX % pointSize;
|
||||||
mouseY -= mouseY % pointSize;
|
mouseY -= mouseY % pointSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
command.X = mouseX;
|
command.X = mouseX;
|
||||||
command.Y = mouseY;
|
command.Y = mouseY;
|
||||||
command.Color = color;
|
command.Color = color;
|
||||||
@ -91,10 +102,11 @@ public class CanvasService {
|
|||||||
|
|
||||||
if (command.Equals(_lastCommand))
|
if (command.Equals(_lastCommand))
|
||||||
return; // Pour pas spammer des commandes si c'est pas pertinent.
|
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;
|
_lastCommand = command;
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ builder.AddServiceDefaults();
|
|||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddRazorComponents()
|
builder.Services.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents();
|
.AddInteractiveServerComponents();
|
||||||
builder.AddRedisOutputCache("cache");
|
builder.AddRedis("cache");
|
||||||
|
|
||||||
builder.Services.AddScoped<CanvasService>();
|
builder.Services.AddScoped<CanvasService>();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user