StompMessage.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace NEIntelligentControl2.Service.WebSocket
  7. {
  8. /// <summary>
  9. /// WebSocket消息
  10. /// </summary>
  11. public class StompMessage
  12. {
  13. private readonly Dictionary<string, string> _headers = new Dictionary<string, string>();
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref = "StompMessage" /> class.
  16. /// </summary>
  17. /// <param name = "command">The command.</param>
  18. public StompMessage(string command)
  19. : this(command, string.Empty)
  20. {
  21. }
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref = "StompMessage" /> class.
  24. /// </summary>
  25. /// <param name = "command">The command.</param>
  26. /// <param name = "body">The body.</param>
  27. public StompMessage(string command, string body)
  28. : this(command, body, new Dictionary<string, string>())
  29. {
  30. }
  31. /// <summary>
  32. /// Initializes a new instance of the <see cref = "StompMessage" /> class.
  33. /// </summary>
  34. /// <param name = "command">The command.</param>
  35. /// <param name = "body">The body.</param>
  36. /// <param name = "headers">The headers.</param>
  37. internal StompMessage(string command, string body, Dictionary<string, string> headers)
  38. {
  39. Command = command;
  40. Body = body;
  41. _headers = headers;
  42. this["content-length"] = body.Length.ToString();
  43. }
  44. public Dictionary<string, string> Headers
  45. {
  46. get { return _headers; }
  47. }
  48. /// <summary>
  49. /// Gets the body.
  50. /// </summary>
  51. public string Body { get; private set; }
  52. /// <summary>
  53. /// Gets the command.
  54. /// </summary>
  55. public string Command { get; private set; }
  56. /// <summary>
  57. /// Gets or sets the specified header attribute.
  58. /// </summary>
  59. public string this[string header]
  60. {
  61. get { return _headers.ContainsKey(header) ? _headers[header] : string.Empty; }
  62. set { _headers[header] = value; }
  63. }
  64. /// <summary>
  65. /// 转换为字符串
  66. /// </summary>
  67. /// <returns></returns>
  68. public override string ToString()
  69. {
  70. var buffer = new StringBuilder();
  71. buffer.Append(Command + "\n");
  72. if (Headers != null)
  73. {
  74. foreach (var header in Headers)
  75. {
  76. buffer.Append(header.Key + ":" + header.Value + "\n");
  77. }
  78. }
  79. buffer.Append("\n");
  80. buffer.Append(Body);
  81. buffer.Append('\0');
  82. return buffer.ToString();
  83. }
  84. /// <summary>
  85. /// 解析消息
  86. /// </summary>
  87. internal static StompMessage Deserialize(string data)
  88. {
  89. var reader = new System.IO.StringReader(data);
  90. var command = reader.ReadLine();
  91. var headers = new Dictionary<string, string>();
  92. var header = reader.ReadLine();
  93. while (!string.IsNullOrEmpty(header))
  94. {
  95. var split = header.Split(':');
  96. if (split.Length == 2) headers[split[0].Trim()] = split[1].Trim();
  97. header = reader.ReadLine() ?? string.Empty;
  98. }
  99. var body = reader.ReadToEnd() ?? string.Empty;
  100. body = body.TrimEnd('\r', '\n', '\0');
  101. return new StompMessage(command, body, headers);
  102. }
  103. }
  104. }