ConnectedMessage.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using SocketIOClient.Transport;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.Json;
  6. namespace SocketIOClient.Messages
  7. {
  8. public class ConnectedMessage : IMessage
  9. {
  10. public MessageType Type => MessageType.Connected;
  11. public string Namespace { get; set; }
  12. public string Sid { get; set; }
  13. public List<byte[]> OutgoingBytes { get; set; }
  14. public List<byte[]> IncomingBytes { get; set; }
  15. public int BinaryCount { get; }
  16. public int Eio { get; set; }
  17. public TransportProtocol Protocol { get; set; }
  18. public IEnumerable<KeyValuePair<string, string>> Query { get; set; }
  19. public string AuthJsonStr { get; set; }
  20. public void Read(string msg)
  21. {
  22. if (Eio == 3)
  23. {
  24. Eio3Read(msg);
  25. }
  26. else
  27. {
  28. Eio4Read(msg);
  29. }
  30. }
  31. public string Write()
  32. {
  33. if (Eio == 3)
  34. {
  35. return Eio3Write();
  36. }
  37. return Eio4Write();
  38. }
  39. public void Eio4Read(string msg)
  40. {
  41. int index = msg.IndexOf('{');
  42. if (index > 0)
  43. {
  44. Namespace = msg.Substring(0, index - 1);
  45. msg = msg.Substring(index);
  46. }
  47. else
  48. {
  49. Namespace = string.Empty;
  50. }
  51. Sid = JsonDocument.Parse(msg).RootElement.GetProperty("sid").GetString();
  52. }
  53. public string Eio4Write()
  54. {
  55. var builder = new StringBuilder("40");
  56. if (!string.IsNullOrEmpty(Namespace))
  57. {
  58. builder.Append(Namespace).Append(',');
  59. }
  60. builder.Append(AuthJsonStr);
  61. return builder.ToString();
  62. }
  63. public void Eio3Read(string msg)
  64. {
  65. if (msg.Length >= 2)
  66. {
  67. int startIndex = msg.IndexOf('/');
  68. if (startIndex == -1)
  69. {
  70. return;
  71. }
  72. int endIndex = msg.IndexOf('?', startIndex);
  73. if (endIndex == -1)
  74. {
  75. endIndex = msg.IndexOf(',', startIndex);
  76. }
  77. if (endIndex == -1)
  78. {
  79. endIndex = msg.Length;
  80. }
  81. Namespace = msg.Substring(startIndex, endIndex);
  82. }
  83. }
  84. public string Eio3Write()
  85. {
  86. if (string.IsNullOrEmpty(Namespace))
  87. {
  88. return string.Empty;
  89. }
  90. var builder = new StringBuilder("40");
  91. builder.Append(Namespace);
  92. if (Query != null)
  93. {
  94. int i = -1;
  95. foreach (var item in Query)
  96. {
  97. i++;
  98. if (i == 0)
  99. {
  100. builder.Append('?');
  101. }
  102. else
  103. {
  104. builder.Append('&');
  105. }
  106. builder.Append(item.Key).Append('=').Append(item.Value);
  107. }
  108. }
  109. builder.Append(',');
  110. return builder.ToString();
  111. }
  112. }
  113. }