ClientBinaryAckMessage.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using SocketIOClient.Transport;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.Json;
  6. namespace SocketIOClient.Messages
  7. {
  8. /// <summary>
  9. /// The server calls the client's callback with binary
  10. /// </summary>
  11. public class ClientBinaryAckMessage : IMessage
  12. {
  13. public MessageType Type => MessageType.BinaryAckMessage;
  14. public string Namespace { get; set; }
  15. public string Event { get; set; }
  16. public List<JsonElement> JsonElements { get; set; }
  17. public string Json { get; set; }
  18. public int Id { get; set; }
  19. public int BinaryCount { get; set; }
  20. public int Eio { get; set; }
  21. public TransportProtocol Protocol { get; set; }
  22. public List<byte[]> OutgoingBytes { get; set; }
  23. public List<byte[]> IncomingBytes { get; set; }
  24. public void Read(string msg)
  25. {
  26. int index1 = msg.IndexOf('-');
  27. BinaryCount = int.Parse(msg.Substring(0, index1));
  28. int index2 = msg.IndexOf('[');
  29. int index3 = msg.LastIndexOf(',', index2);
  30. if (index3 > -1)
  31. {
  32. Namespace = msg.Substring(index1 + 1, index3 - index1 - 1);
  33. Id = int.Parse(msg.Substring(index3 + 1, index2 - index3 - 1));
  34. }
  35. else
  36. {
  37. Id = int.Parse(msg.Substring(index1 + 1, index2 - index1 - 1));
  38. }
  39. string json = msg.Substring(index2);
  40. JsonElements = JsonDocument.Parse(json).RootElement.EnumerateArray().ToList();
  41. }
  42. public string Write()
  43. {
  44. var builder = new StringBuilder();
  45. builder
  46. .Append("45")
  47. .Append(OutgoingBytes.Count)
  48. .Append('-');
  49. if (!string.IsNullOrEmpty(Namespace))
  50. {
  51. builder.Append(Namespace).Append(',');
  52. }
  53. builder.Append(Id);
  54. if (string.IsNullOrEmpty(Json))
  55. {
  56. builder.Append("[\"").Append(Event).Append("\"]");
  57. }
  58. else
  59. {
  60. string data = Json.Insert(1, $"\"{Event}\",");
  61. builder.Append(data);
  62. }
  63. return builder.ToString();
  64. }
  65. }
  66. }