OH YEAH.
This commit is contained in:
		| @@ -18,7 +18,6 @@ public class CanvasService { | |||||||
|     private IRedisService _redisService; |     private IRedisService _redisService; | ||||||
|     private CanvasCommand _lastCommand = new(); |     private CanvasCommand _lastCommand = new(); | ||||||
|     private List<CanvasCommand> _lsComms = new(); |     private List<CanvasCommand> _lsComms = new(); | ||||||
|     private Guid _id = Guid.NewGuid(); |  | ||||||
|     private bool _is_init = false,  |     private bool _is_init = false,  | ||||||
|                  _is_started = false,  |                  _is_started = false,  | ||||||
|                  _has_ended = false; |                  _has_ended = false; | ||||||
| @@ -55,7 +54,7 @@ public class CanvasService { | |||||||
|         while (!_has_ended) { |         while (!_has_ended) { | ||||||
|             if (_lsComms.Count > 0) |             if (_lsComms.Count > 0) | ||||||
|                 _lsComms.Clear(); |                 _lsComms.Clear(); | ||||||
|             _lsComms.AddRange(await _redisService.Consume(_id.ToString())); |             _lsComms.AddRange(await _redisService.Consume()); | ||||||
|             if (_lsComms.Count == 1) |             if (_lsComms.Count == 1) | ||||||
|                 Draw(_lsComms[0]); |                 Draw(_lsComms[0]); | ||||||
|             else if (_lsComms.Count > 0)  |             else if (_lsComms.Count > 0)  | ||||||
|   | |||||||
| @@ -8,5 +8,5 @@ public interface IRedisService { | |||||||
|     void Publish(CanvasCommand command); |     void Publish(CanvasCommand command); | ||||||
|  |  | ||||||
|     void Produce(CanvasCommand command); |     void Produce(CanvasCommand command); | ||||||
|     Task<IEnumerable<CanvasCommand>> Consume(string id); |     Task<IEnumerable<CanvasCommand>> Consume(); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -9,9 +9,11 @@ namespace BlazorCanvas.Server.Components.Data; | |||||||
| // https://developer.redis.com/develop/dotnet/streams/stream-basics/ | // https://developer.redis.com/develop/dotnet/streams/stream-basics/ | ||||||
|  |  | ||||||
| public class RedisService : IRedisService { | public class RedisService : IRedisService { | ||||||
|     const string STREAM_NAME = "steamie", |     private const string STREAM_NAME = "steamie", | ||||||
|                  GROUP_NAME = "groupie", |                          GROUP_NAME = "groupie", | ||||||
|                  SUB_NAME = "servant"; |                          SUB_NAME = "servant"; | ||||||
|  |  | ||||||
|  |     private string _lastId = "0-0"; | ||||||
|  |  | ||||||
|     private NameValueEntry[] arNve = new NameValueEntry[1]; |     private NameValueEntry[] arNve = new NameValueEntry[1]; | ||||||
|  |  | ||||||
| @@ -29,10 +31,11 @@ public class RedisService : IRedisService { | |||||||
|     public bool InitSubscriber() { |     public bool InitSubscriber() { | ||||||
|         try { |         try { | ||||||
|             _channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal(SUB_NAME)); |             _channel = _cache.GetSubscriber().Subscribe(RedisChannel.Literal(SUB_NAME)); | ||||||
|         } catch (Exception ex) { |         } catch { | ||||||
|             Console.WriteLine(ex); |  | ||||||
|             return false; |             return false; | ||||||
|         } |         } | ||||||
|  |         if (_channel is null) | ||||||
|  |             return false; | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -43,14 +46,11 @@ public class RedisService : IRedisService { | |||||||
|     public async Task<bool> InitStreamer() { |     public async Task<bool> InitStreamer() { | ||||||
|         try { |         try { | ||||||
|             _database = _cache.GetDatabase(); |             _database = _cache.GetDatabase(); | ||||||
|             if (!(await _database.KeyExistsAsync(STREAM_NAME)) || |         } catch { | ||||||
|                  (await _database.StreamGroupInfoAsync(STREAM_NAME)).All(x => x.Name != GROUP_NAME)) { |  | ||||||
|                 await _database.StreamCreateConsumerGroupAsync(STREAM_NAME, GROUP_NAME, "0-0", true); |  | ||||||
|             } |  | ||||||
|         } catch (Exception ex) { |  | ||||||
|             Console.WriteLine(ex); |  | ||||||
|             return false; |             return false; | ||||||
|         } |         } | ||||||
|  |         if (_database is null) | ||||||
|  |             return false; | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -59,13 +59,13 @@ public class RedisService : IRedisService { | |||||||
|         _database.StreamAddAsync(STREAM_NAME, arNve); |         _database.StreamAddAsync(STREAM_NAME, arNve); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public async Task<IEnumerable<CanvasCommand>> Consume(string id) { |     public async Task<IEnumerable<CanvasCommand>> Consume() { | ||||||
|         List<CanvasCommand> lsComm = new(); |         List<CanvasCommand> lsComm = new(); | ||||||
|         CanvasCommand? comm; |         CanvasCommand? comm; | ||||||
|         var result = await _database.StreamReadGroupAsync(STREAM_NAME, GROUP_NAME, id, ">", 100); |         var result = await _database.StreamReadAsync(STREAM_NAME, _lastId, 100); | ||||||
|         string json = ""; |         string json = ""; | ||||||
|  |         bool ok = false; | ||||||
|         foreach (var c in result) { |         foreach (var c in result) { | ||||||
|             _database.StreamAcknowledgeAsync(STREAM_NAME, GROUP_NAME, c.Id); |  | ||||||
|             try { |             try { | ||||||
|                 json = c.Values.FirstOrDefault(x => x.Name == "command").Value.ToString(); |                 json = c.Values.FirstOrDefault(x => x.Name == "command").Value.ToString(); | ||||||
|                 comm = JsonConvert.DeserializeObject<CanvasCommand>(json); |                 comm = JsonConvert.DeserializeObject<CanvasCommand>(json); | ||||||
| @@ -74,7 +74,11 @@ public class RedisService : IRedisService { | |||||||
|                 continue; } |                 continue; } | ||||||
|             if (comm is not null) |             if (comm is not null) | ||||||
|                 lsComm.Add(comm); |                 lsComm.Add(comm); | ||||||
|  |             if (!ok) | ||||||
|  |                 ok = true; | ||||||
|         } |         } | ||||||
|  |         if (ok) | ||||||
|  |             _lastId = result.LastOrDefault().Id.ToString()?? "0-0"; | ||||||
|         return lsComm; |         return lsComm; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user