Séparation des services
This commit is contained in:
@@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace BlazorCanvas.Server.Components.Data;
|
||||
|
||||
@@ -14,15 +13,13 @@ namespace BlazorCanvas.Server.Components.Data;
|
||||
public class CanvasService {
|
||||
private Canvas2DContext? _currentCanvasContext;
|
||||
private IJSRuntime _jsRuntime;
|
||||
private IConnectionMultiplexer _cache;
|
||||
private ChannelMessageQueue _channel;
|
||||
private IRedisService _redisService;
|
||||
private CanvasCommand _lastCommand = new();
|
||||
private bool _is_started = false;
|
||||
|
||||
public CanvasService(IJSRuntime jsRuntime, IConnectionMultiplexer cache) {
|
||||
public CanvasService(IJSRuntime jsRuntime, IRedisService redisService) {
|
||||
_jsRuntime = jsRuntime;
|
||||
_cache = cache;
|
||||
_channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal("lol"));
|
||||
_redisService = redisService;
|
||||
}
|
||||
|
||||
public string currentColor { get; set; } = "Black";
|
||||
@@ -31,12 +28,13 @@ public class CanvasService {
|
||||
public ElementReference divCanvas { get; set; }
|
||||
public BECanvasComponent myCanvas { get; set; } = new();
|
||||
|
||||
public async void Consume() {
|
||||
/// <summary>
|
||||
/// Version Pub/Sub
|
||||
/// </summary>
|
||||
public async void Subscribe() {
|
||||
CancellationToken cToken = new();
|
||||
|
||||
while (!cToken.IsCancellationRequested) {
|
||||
var mess = await _channel.ReadAsync(cToken);
|
||||
var comm = JsonConvert.DeserializeObject<CanvasCommand>(mess.Message);
|
||||
var comm = await _redisService.Subscribe(cToken);
|
||||
if (comm is not null)
|
||||
Draw(comm);
|
||||
}
|
||||
@@ -60,7 +58,12 @@ public class CanvasService {
|
||||
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
|
||||
await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080);
|
||||
}
|
||||
await _currentCanvasContext.SetFillStyleAsync(command.Color);
|
||||
try {
|
||||
await _currentCanvasContext.SetFillStyleAsync(command.Color);
|
||||
} catch (JSDisconnectedException e) {
|
||||
Console.WriteLine(e.ToString());
|
||||
return; // Welp.
|
||||
}
|
||||
await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize);
|
||||
}
|
||||
|
||||
@@ -68,7 +71,7 @@ public class CanvasService {
|
||||
double mouseX = 0, mouseY = 0;
|
||||
|
||||
if (!_is_started) {
|
||||
Consume();
|
||||
Subscribe();
|
||||
_is_started = true;
|
||||
}
|
||||
|
||||
@@ -102,8 +105,8 @@ public class CanvasService {
|
||||
|
||||
if (command.Equals(_lastCommand))
|
||||
return; // Pour pas spammer des commandes si c'est pas pertinent.
|
||||
|
||||
_cache.GetSubscriber().Publish(RedisChannel.Literal("lol"), JsonConvert.SerializeObject(command));
|
||||
|
||||
_redisService.Publish(command);
|
||||
|
||||
//Draw(command); // Local
|
||||
|
||||
|
@@ -0,0 +1,6 @@
|
||||
namespace BlazorCanvas.Server.Components.Data;
|
||||
|
||||
public interface IRedisService {
|
||||
public Task<CanvasCommand?> Subscribe(CancellationToken cToken);
|
||||
public void Publish(CanvasCommand command);
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace BlazorCanvas.Server.Components.Data;
|
||||
|
||||
// https://developer.redis.com/develop/dotnet/streams/stream-basics/
|
||||
|
||||
public class RedisService: IRedisService {
|
||||
private IConnectionMultiplexer _cache;
|
||||
private ChannelMessageQueue _channel;
|
||||
|
||||
public RedisService(IConnectionMultiplexer cache) {
|
||||
_cache = cache;
|
||||
_channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal("lol"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Version Pub/Sub
|
||||
/// </summary>
|
||||
public async Task<CanvasCommand?> Subscribe(CancellationToken cToken) {
|
||||
var mess = await _channel.ReadAsync(cToken);
|
||||
var comm = JsonConvert.DeserializeObject<CanvasCommand>(mess.Message);
|
||||
if (comm is not null)
|
||||
return comm;
|
||||
else return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Version Pub/Sub
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
public async void Publish(CanvasCommand command) {
|
||||
CanvasCommand cm = new(command);
|
||||
await _cache.GetSubscriber().PublishAsync(_channel.Channel, JsonConvert.SerializeObject(cm));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user