Program.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using FreeRedis;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace WCS.DebugTool
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var key = "";
  15. RedisClient redis = null;
  16. var last = DateTime.MinValue;
  17. Task.Run(() =>
  18. {
  19. SpinWait.SpinUntil(() => redis != null);
  20. while (true)
  21. {
  22. try
  23. {
  24. if ((DateTime.Now - last).TotalMilliseconds < 5000 || key == "")
  25. {
  26. Task.Delay(100).Wait();
  27. }
  28. else
  29. {
  30. last = DateTime.Now;
  31. redis.Publish("Login", key);
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. Task.Delay(100).Wait();
  37. }
  38. }
  39. });
  40. while (true)
  41. {
  42. if (redis == null)
  43. {
  44. Console.WriteLine("请输入订阅关键字(逗号分隔):");
  45. redis = new RedisClient(Properties.Settings.Default.Redis);
  46. key = Console.ReadLine();
  47. last = DateTime.MinValue;
  48. var arr = key.Split(',');
  49. redis.PSubscribe(arr, (channel, msg) =>
  50. {
  51. Console.WriteLine($"----------对象:{channel}----------\n{msg}");
  52. });
  53. }
  54. else
  55. {
  56. if (Console.ReadKey().Key == ConsoleKey.Escape)
  57. {
  58. Console.Clear();
  59. redis.Dispose();
  60. redis = null;
  61. key = "";
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }