SimpleWebChatServerModule.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections.Concurrent;
  2. using System.Net;
  3. using System.Net.WebSockets;
  4. using System.Text;
  5. namespace SimpleWebChat.ConHost
  6. {
  7. public static class SimpleWebChatServerModule
  8. {
  9. private static readonly ConcurrentDictionary<int, WebSocket> TextChatSessions = new();
  10. private static readonly List<byte[]> HistoryMessage = new();
  11. public static void ProcessRequest(HttpListenerContext context, int currentSessionId)
  12. {
  13. WebSocket sck = null;
  14. try
  15. {
  16. var wsc = context.AcceptWebSocketAsync("swcp").Result;
  17. Console.WriteLine($"Request #{currentSessionId:00000} TextChat WebSocket Session Start");
  18. sck = TextChatSessions[currentSessionId] = wsc.WebSocket;
  19. var buffer = new byte[1024];
  20. var r = sck.ReceiveAsync(buffer, default).Result;
  21. var s = Encoding.UTF8.GetString(buffer, 0, r.Count);
  22. byte[][] copy;
  23. lock (HistoryMessage) copy = HistoryMessage.ToArray();
  24. foreach (var item in copy)
  25. {
  26. sck.SendAsync(item, WebSocketMessageType.Text, true, default).Wait();
  27. }
  28. sck.BroadCastFaf($"SYS{Environment.NewLine}" +
  29. $" Session #{currentSessionId:X4}({s}) Connected.{Environment.NewLine}" +
  30. $" Now number of online session: {TextChatSessions.Count}");
  31. while (true)
  32. {
  33. r = sck.ReceiveAsync(buffer, default).Result;
  34. if (r.Count == 0)
  35. {
  36. break;
  37. }
  38. else
  39. {
  40. s = Encoding.UTF8.GetString(buffer, 0, r.Count);
  41. sck.BroadCastFaf($"#{currentSessionId:X4}{Environment.NewLine} {s}");
  42. }
  43. }
  44. }
  45. catch (Exception)
  46. {
  47. try
  48. {
  49. if (sck != null && sck.State != WebSocketState.Aborted)
  50. {
  51. sck.CloseAsync(WebSocketCloseStatus.InternalServerError, "Error", default).Wait();
  52. }
  53. }
  54. catch (Exception e)
  55. {
  56. Console.WriteLine(e);
  57. }
  58. }
  59. TextChatSessions.TryRemove(currentSessionId, out _);
  60. sck.BroadCastFaf($"SYS{Environment.NewLine}" +
  61. $" Session #{currentSessionId:X4} Disconnected.{Environment.NewLine}" +
  62. $" Now number of online session: {TextChatSessions.Count}");
  63. }
  64. private static void BroadCastFaf(this WebSocket sck, string content)
  65. {
  66. var now = DateTime.Now;
  67. string text = $"{now} {content}";
  68. var buf = Encoding.UTF8.GetBytes(text);
  69. lock (HistoryMessage)
  70. {
  71. HistoryMessage.Add(buf);
  72. while (HistoryMessage.Count >= ConfigFile.Instance.HistoryMessageLength)
  73. {
  74. HistoryMessage.RemoveAt(0);
  75. }
  76. }
  77. var all = TextChatSessions.Values.ToArray();
  78. foreach (var s in all)
  79. {
  80. try
  81. {
  82. s.SendAsync(text);
  83. }
  84. catch
  85. {
  86. //FUCK ERR
  87. }
  88. }
  89. }
  90. private static Task SendAsync(this WebSocket sck, string text) => sck.SendAsync(Encoding.UTF8.GetBytes(text), WebSocketMessageType.Text, true, default);
  91. }
  92. }