NegotiationResult.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.Collections.Generic;
  3. namespace BestHTTP.SignalRCore.Messages
  4. {
  5. public sealed class SupportedTransport
  6. {
  7. /// <summary>
  8. /// Name of the transport.
  9. /// </summary>
  10. public string Name { get; private set; }
  11. /// <summary>
  12. /// Supported transfer formats of the transport.
  13. /// </summary>
  14. public List<string> SupportedFormats { get; private set; }
  15. internal SupportedTransport(string transportName, List<string> transferFormats)
  16. {
  17. this.Name = transportName;
  18. this.SupportedFormats = transferFormats;
  19. }
  20. }
  21. /// <summary>
  22. /// Negotiation result of the /negotiation request.
  23. /// <see cref="https://github.com/aspnet/SignalR/blob/dev/specs/TransportProtocols.md#post-endpoint-basenegotiate-request"/>
  24. /// </summary>
  25. public sealed class NegotiationResult
  26. {
  27. /// <summary>
  28. /// The connectionId which is required by the Long Polling and Server-Sent Events transports (in order to correlate sends and receives).
  29. /// </summary>
  30. public string ConnectionId { get; private set; }
  31. /// <summary>
  32. /// The availableTransports list which describes the transports the server supports. For each transport, the name of the transport (transport) is listed, as is a list of "transfer formats" supported by the transport (transferFormats)
  33. /// </summary>
  34. public List<SupportedTransport> SupportedTransports { get; private set; }
  35. /// <summary>
  36. /// The url which is the URL the client should connect to.
  37. /// </summary>
  38. public Uri Url { get; private set; }
  39. /// <summary>
  40. /// The accessToken which is an optional bearer token for accessing the specified url.
  41. /// </summary>
  42. public string AccessToken { get; private set; }
  43. internal static NegotiationResult Parse(string json, out string error)
  44. {
  45. error = null;
  46. Dictionary<string, object> response = BestHTTP.JSON.Json.Decode(json) as Dictionary<string, object>;
  47. if (response == null)
  48. {
  49. error = "Json decoding failed!";
  50. return null;
  51. }
  52. try
  53. {
  54. NegotiationResult result = new NegotiationResult();
  55. object value;
  56. if (response.TryGetValue("connectionId", out value))
  57. result.ConnectionId = value.ToString();
  58. if (response.TryGetValue("availableTransports", out value))
  59. {
  60. List<object> transports = value as List<object>;
  61. if (transports != null)
  62. {
  63. List<SupportedTransport> supportedTransports = new List<SupportedTransport>(transports.Count);
  64. foreach (Dictionary<string, object> transport in transports)
  65. {
  66. string transportName = string.Empty;
  67. List<string> transferModes = null;
  68. if (transport.TryGetValue("transport", out value))
  69. transportName = value.ToString();
  70. if (transport.TryGetValue("transferFormats", out value))
  71. {
  72. List<object> transferFormats = value as List<object>;
  73. if (transferFormats != null)
  74. {
  75. transferModes = new List<string>(transferFormats.Count);
  76. foreach (var mode in transferFormats)
  77. transferModes.Add(mode.ToString());
  78. }
  79. }
  80. supportedTransports.Add(new SupportedTransport(transportName, transferModes));
  81. }
  82. result.SupportedTransports = supportedTransports;
  83. }
  84. }
  85. if (response.TryGetValue("url", out value))
  86. result.Url = new Uri(value.ToString());
  87. if (response.TryGetValue("accessToken", out value))
  88. result.AccessToken = value.ToString();
  89. return result;
  90. }
  91. catch (Exception ex)
  92. {
  93. error = "Error while parsing result: " + ex.Message + " StackTrace: " + ex.StackTrace;
  94. return null;
  95. }
  96. }
  97. }
  98. }