1
0

Ajouts du mode stream + ajouts QoL.

This commit is contained in:
MarcEricMartel 2023-11-18 12:19:37 -05:00
parent 4309ab9aad
commit 2bea742a69
3 changed files with 51 additions and 11 deletions

View File

@ -5,6 +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.ComponentModel;
namespace BlazorCanvas.Server.Components.Data; namespace BlazorCanvas.Server.Components.Data;
@ -15,13 +16,21 @@ public class CanvasService {
private IJSRuntime _jsRuntime; private IJSRuntime _jsRuntime;
private IRedisService _redisService; private IRedisService _redisService;
private CanvasCommand _lastCommand = new(); private CanvasCommand _lastCommand = new();
private bool _is_started = false; private List<CanvasCommand> _lsComms = new();
private bool _is_init = false,
_is_started = false,
_has_ended = false;
public CanvasService(IJSRuntime jsRuntime, IRedisService redisService) { public CanvasService(IJSRuntime jsRuntime, IRedisService redisService) {
_jsRuntime = jsRuntime; _jsRuntime = jsRuntime;
_redisService = redisService; _redisService = redisService;
} }
~CanvasService() {
_has_ended = true;
Task.Delay(100);
}
public string currentColor { get; set; } = "Black"; public string currentColor { get; set; } = "Black";
public int pointSize { get; set; } = 1; public int pointSize { get; set; } = 1;
public bool snap { get; set; } 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<CanvasCommand> lsCommand) { public async void Draw(IEnumerable<CanvasCommand> lsCommand) {
if (_currentCanvasContext is null) { if (_currentCanvasContext is null) {
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); _currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
@ -71,7 +100,10 @@ public class CanvasService {
double mouseX = 0, mouseY = 0; double mouseX = 0, mouseY = 0;
if (!_is_started) { if (!_is_started) {
Subscribe(); if (!_is_init)
Init();
if (_is_init)
Consume();
_is_started = true; _is_started = true;
} }
@ -106,7 +138,9 @@ 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.
_redisService.Publish(command); _redisService.Produce(command); // Stream
// _redisService.Publish(command); // Pub/Sub
// Draw(command); // Local // Draw(command); // Local

View File

@ -1,6 +1,12 @@
namespace BlazorCanvas.Server.Components.Data; namespace BlazorCanvas.Server.Components.Data;
public interface IRedisService { public interface IRedisService {
public Task<CanvasCommand?> Subscribe(CancellationToken cToken); Task<bool> InitStreamer();
public void Publish(CanvasCommand command); bool InitSubscriber();
Task<CanvasCommand?> Subscribe(CancellationToken cToken);
void Publish(CanvasCommand command);
void Produce(CanvasCommand command);
Task<IEnumerable<CanvasCommand>> Consume();
} }

View File

@ -13,7 +13,7 @@ public class RedisService : IRedisService {
GROUP_NAME = "groupie", GROUP_NAME = "groupie",
SUB_NAME = "servant"; SUB_NAME = "servant";
private NameValueEntry[] arnve = new NameValueEntry[1]; private NameValueEntry[] arNve = new NameValueEntry[1];
private IConnectionMultiplexer _cache; private IConnectionMultiplexer _cache;
private IDatabase _database; private IDatabase _database;
@ -55,8 +55,8 @@ public class RedisService : IRedisService {
} }
public async void Produce(CanvasCommand command) { public async void Produce(CanvasCommand command) {
arnve[0] = new NameValueEntry("command", JsonConvert.SerializeObject(command)); arNve[0] = new NameValueEntry("command", JsonConvert.SerializeObject(command));
await _database.StreamAddAsync(STREAM_NAME, arnve); await _database.StreamAddAsync(STREAM_NAME, arNve);
} }
public async Task<IEnumerable<CanvasCommand>> Consume() { public async Task<IEnumerable<CanvasCommand>> Consume() {