Ajouts du mode stream + ajouts QoL.
This commit is contained in:
parent
4309ab9aad
commit
2bea742a69
@ -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<CanvasCommand> _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<CanvasCommand> lsCommand) {
|
||||
if (_currentCanvasContext is null) {
|
||||
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
|
||||
@ -71,7 +100,10 @@ public class CanvasService {
|
||||
double mouseX = 0, mouseY = 0;
|
||||
|
||||
if (!_is_started) {
|
||||
Subscribe();
|
||||
if (!_is_init)
|
||||
Init();
|
||||
if (_is_init)
|
||||
Consume();
|
||||
_is_started = true;
|
||||
}
|
||||
|
||||
@ -106,9 +138,11 @@ public class CanvasService {
|
||||
if (command.Equals(_lastCommand))
|
||||
return; // Pour pas spammer des commandes si c'est pas pertinent.
|
||||
|
||||
_redisService.Publish(command);
|
||||
_redisService.Produce(command); // Stream
|
||||
|
||||
//Draw(command); // Local
|
||||
// _redisService.Publish(command); // Pub/Sub
|
||||
|
||||
// Draw(command); // Local
|
||||
|
||||
_lastCommand = command;
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
namespace BlazorCanvas.Server.Components.Data;
|
||||
|
||||
public interface IRedisService {
|
||||
public Task<CanvasCommand?> Subscribe(CancellationToken cToken);
|
||||
public void Publish(CanvasCommand command);
|
||||
Task<bool> InitStreamer();
|
||||
bool InitSubscriber();
|
||||
|
||||
Task<CanvasCommand?> Subscribe(CancellationToken cToken);
|
||||
void Publish(CanvasCommand command);
|
||||
|
||||
void Produce(CanvasCommand command);
|
||||
Task<IEnumerable<CanvasCommand>> Consume();
|
||||
}
|
||||
|
@ -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<IEnumerable<CanvasCommand>> Consume() {
|
||||
|
Loading…
Reference in New Issue
Block a user