SocketOptions.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #if !BESTHTTP_DISABLE_SOCKETIO
  2. using System;
  3. using System.Text;
  4. using PlatformSupport.Collections.ObjectModel;
  5. #if !NETFX_CORE
  6. using PlatformSupport.Collections.Specialized;
  7. #else
  8. using System.Collections.Specialized;
  9. #endif
  10. namespace BestHTTP.SocketIO3
  11. {
  12. public delegate void HTTPRequestCallbackDelegate(SocketManager manager, HTTPRequest request);
  13. public sealed class SocketOptions
  14. {
  15. #region Properties
  16. /// <summary>
  17. /// The SocketManager will try to connect with this transport.
  18. /// </summary>
  19. public Transports.TransportTypes ConnectWith { get; set; }
  20. /// <summary>
  21. /// Whether to reconnect automatically after a disconnect (default true)
  22. /// </summary>
  23. public bool Reconnection { get; set; }
  24. /// <summary>
  25. /// Number of attempts before giving up (default Int.MaxValue)
  26. /// </summary>
  27. public int ReconnectionAttempts { get; set; }
  28. /// <summary>
  29. /// How long to initially wait before attempting a new reconnection (default 1000ms).
  30. /// Affected by +/- RandomizationFactor, for example the default initial delay will be between 500ms to 1500ms.
  31. /// </summary>
  32. public TimeSpan ReconnectionDelay { get; set; }
  33. /// <summary>
  34. /// Maximum amount of time to wait between reconnections (default 5000ms).
  35. /// Each attempt increases the reconnection delay along with a randomization as above.
  36. /// </summary>
  37. public TimeSpan ReconnectionDelayMax { get; set; }
  38. /// <summary>
  39. /// (default 0.5`), [0..1]
  40. /// </summary>
  41. public float RandomizationFactor { get { return randomizationFactor; } set { randomizationFactor = Math.Min(1.0f, Math.Max(0.0f, value)); } }
  42. private float randomizationFactor;
  43. /// <summary>
  44. /// Connection timeout before a connect_error and connect_timeout events are emitted (default 20000ms)
  45. /// </summary>
  46. public TimeSpan Timeout { get; set; }
  47. /// <summary>
  48. /// By setting this false, you have to call SocketManager's Open() whenever you decide it's appropriate.
  49. /// </summary>
  50. public bool AutoConnect { get; set; }
  51. /// <summary>
  52. /// Additional query parameters that will be passed for accessed uris. If the value is null, or an empty string it will be not appended to the query only the key.
  53. /// <remarks>The keys and values must be escaped properly, as the plugin will not escape these. </remarks>
  54. /// </summary>
  55. public ObservableDictionary<string, string> AdditionalQueryParams
  56. {
  57. get { return additionalQueryParams; }
  58. set
  59. {
  60. // Unsubscribe from previous dictionary's events
  61. if (additionalQueryParams != null)
  62. additionalQueryParams.CollectionChanged -= AdditionalQueryParams_CollectionChanged;
  63. additionalQueryParams = value;
  64. // Clear out the cached value
  65. BuiltQueryParams = null;
  66. // Subscribe to the collection changed event
  67. if (value != null)
  68. value.CollectionChanged += AdditionalQueryParams_CollectionChanged;
  69. }
  70. }
  71. private ObservableDictionary<string, string> additionalQueryParams;
  72. /// <summary>
  73. /// If it's false, the parameters in the AdditionalQueryParams will be passed for all HTTP requests. Its default value is true.
  74. /// </summary>
  75. public bool QueryParamsOnlyForHandshake { get; set; }
  76. /// <summary>
  77. /// A callback that called for every HTTPRequest the socket.io protocol sends out. It can be used to further customize (add additional request for example) requests.
  78. /// </summary>
  79. public HTTPRequestCallbackDelegate HTTPRequestCustomizationCallback { get; set; }
  80. /// <summary>
  81. /// Starting with Socket.IO v3, connecting to a namespace a client can send payload data. When the Auth callback function is set, the plugin going to call it when connecting to a namespace. Its return value must be a json string!
  82. /// </summary>
  83. public Func<SocketManager, Socket, object> Auth;
  84. #endregion
  85. /// <summary>
  86. /// The cached value of the result of the BuildQueryParams() call.
  87. /// </summary>
  88. private string BuiltQueryParams;
  89. /// <summary>
  90. /// Constructor, setting the default option values.
  91. /// </summary>
  92. public SocketOptions()
  93. {
  94. ConnectWith = Transports.TransportTypes.Polling;
  95. Reconnection = true;
  96. ReconnectionAttempts = int.MaxValue;
  97. ReconnectionDelay = TimeSpan.FromMilliseconds(1000);
  98. ReconnectionDelayMax = TimeSpan.FromMilliseconds(5000);
  99. RandomizationFactor = 0.5f;
  100. Timeout = TimeSpan.FromMilliseconds(20000);
  101. AutoConnect = true;
  102. QueryParamsOnlyForHandshake = true;
  103. }
  104. #region Helper Functions
  105. /// <summary>
  106. /// Builds the keys and values from the AdditionalQueryParams to an key=value form. If AdditionalQueryParams is null or empty, it will return an empty string.
  107. /// </summary>
  108. internal string BuildQueryParams()
  109. {
  110. if (AdditionalQueryParams == null || AdditionalQueryParams.Count == 0)
  111. return string.Empty;
  112. if (!string.IsNullOrEmpty(BuiltQueryParams))
  113. return BuiltQueryParams;
  114. StringBuilder sb = new StringBuilder(AdditionalQueryParams.Count * 4);
  115. foreach(var kvp in AdditionalQueryParams)
  116. {
  117. sb.Append("&");
  118. sb.Append(kvp.Key);
  119. if (!string.IsNullOrEmpty(kvp.Value))
  120. {
  121. sb.Append("=");
  122. sb.Append(kvp.Value);
  123. }
  124. }
  125. return BuiltQueryParams = sb.ToString();
  126. }
  127. /// <summary>
  128. /// This event will be called when the AdditonalQueryPrams dictionary changed. We have to reset the cached values.
  129. /// </summary>
  130. private void AdditionalQueryParams_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  131. {
  132. BuiltQueryParams = null;
  133. }
  134. #endregion
  135. }
  136. }
  137. #endif