You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

82 line
2.2 KiB

  1. using JT1078.Gateway.Metadata;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.WebSockets;
  7. using System.Text;
  8. using System.Threading;
  9. namespace JT1078.Gateway.Sessions
  10. {
  11. public class JT1078HttpSessionManager
  12. {
  13. public ConcurrentDictionary<string, JT1078HttpContext> Sessions { get; }
  14. public JT1078HttpSessionManager()
  15. {
  16. Sessions = new ConcurrentDictionary<string, JT1078HttpContext>();
  17. }
  18. public bool TryAdd(JT1078HttpContext httpContext)
  19. {
  20. return Sessions.TryAdd(httpContext.SessionId, httpContext);
  21. }
  22. public bool TryRemove(string sessionId)
  23. {
  24. //todo:session close notice
  25. return Sessions.TryRemove(sessionId,out JT1078HttpContext session);
  26. }
  27. public void SendHttpChunk(byte[] data)
  28. {
  29. //todo:set http chunk
  30. //todo:session close notice
  31. //byte[] b = Encoding.UTF8.GetBytes("ack");
  32. //context.Response.StatusCode = 200;
  33. //context.Response.KeepAlive = true;
  34. //context.Response.ContentLength64 = b.Length;
  35. //await context.Response.OutputStream.WriteAsync(b, 0, b.Length);
  36. //context.Response.Close();
  37. }
  38. public int SessionCount
  39. {
  40. get
  41. {
  42. return Sessions.Count;
  43. }
  44. }
  45. public List<JT1078HttpContext> GetAll()
  46. {
  47. return Sessions.Select(s => s.Value).ToList();
  48. }
  49. internal void TryRemoveAll()
  50. {
  51. foreach(var item in Sessions)
  52. {
  53. try
  54. {
  55. if (item.Value.IsWebSocket)
  56. {
  57. item.Value.WebSocketContext.WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "server close", CancellationToken.None);
  58. }
  59. else
  60. {
  61. item.Value.Context.Response.Close();
  62. }
  63. }
  64. catch (Exception)
  65. {
  66. }
  67. }
  68. }
  69. }
  70. }