SocketOptions.cs 6.6 KB

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