ClientAckMessage.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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
  10. /// </summary>
  11. public class ClientAckMessage : IMessage
  12. {
  13. public MessageType Type => MessageType.AckMessage;
  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 List<byte[]> OutgoingBytes { get; set; }
  20. public List<byte[]> IncomingBytes { get; set; }
  21. public int BinaryCount { get; }
  22. public int Eio { get; set; }
  23. public TransportProtocol Protocol { get; set; }
  24. public void Read(string msg)
  25. {
  26. int index = msg.IndexOf('[');
  27. int lastIndex = msg.LastIndexOf(',', index);
  28. if (lastIndex > -1)
  29. {
  30. string text = msg.Substring(0, index);
  31. Namespace = text.Substring(0, lastIndex);
  32. Id = int.Parse(text.Substring(lastIndex + 1));
  33. }
  34. else
  35. {
  36. Id = int.Parse(msg.Substring(0, index));
  37. }
  38. msg = msg.Substring(index);
  39. JsonElements = JsonDocument.Parse(msg).RootElement.EnumerateArray().ToList();
  40. }
  41. public string Write()
  42. {
  43. var builder = new StringBuilder();
  44. builder.Append("42");
  45. if (!string.IsNullOrEmpty(Namespace))
  46. {
  47. builder.Append(Namespace).Append(',');
  48. }
  49. builder.Append(Id);
  50. if (string.IsNullOrEmpty(Json))
  51. {
  52. builder.Append("[\"").Append(Event).Append("\"]");
  53. }
  54. else
  55. {
  56. string data = Json.Insert(1, $"\"{Event}\",");
  57. builder.Append(data);
  58. }
  59. return builder.ToString();
  60. }
  61. }
  62. }