PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/SignalR.Client/Hubs/HubConnection.cs

https://github.com/kpmrafeeq/SignalR
C# | 145 lines | 89 code | 17 blank | 39 comment | 8 complexity | 77de0a80c645dc38d14d2fdd78151893 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using SignalR.Client.Transports;
  8. namespace SignalR.Client.Hubs
  9. {
  10. /// <summary>
  11. /// A <see cref="Connection"/> for interacting with Hubs.
  12. /// </summary>
  13. public class HubConnection : Connection
  14. {
  15. private readonly Dictionary<string, HubProxy> _hubs = new Dictionary<string, HubProxy>(StringComparer.OrdinalIgnoreCase);
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="HubConnection"/> class.
  18. /// </summary>
  19. /// <param name="url">The url to connect to.</param>
  20. public HubConnection(string url)
  21. : this(url, useDefaultUrl: true)
  22. {
  23. }
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="HubConnection"/> class.
  26. /// </summary>
  27. /// <param name="url">The url to connect to.</param>
  28. /// <param name="useDefaultUrl">Determines if the default "/signalr" path should be appended to the specified url.</param>
  29. public HubConnection(string url, bool useDefaultUrl)
  30. : base(GetUrl(url, useDefaultUrl))
  31. {
  32. }
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="HubConnection"/> class.
  35. /// </summary>
  36. /// <param name="url">The url to connect to.</param>
  37. /// <param name="queryString">The query string data to pass to the server.</param>
  38. public HubConnection(string url, string queryString)
  39. : this(url, queryString, useDefaultUrl: true)
  40. {
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="HubConnection"/> class.
  44. /// </summary>
  45. /// <param name="url">The url to connect to.</param>
  46. /// <param name="queryString">The query string data to pass to the server.</param>
  47. /// <param name="useDefaultUrl">Determines if the default "/signalr" path should be appended to the specified url.</param>
  48. public HubConnection(string url, string queryString, bool useDefaultUrl)
  49. : base(GetUrl(url, useDefaultUrl), queryString)
  50. {
  51. }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="HubConnection"/> class.
  54. /// </summary>
  55. /// <param name="url">The url to connect to.</param>
  56. /// <param name="queryString">The query string data to pass to the server.</param>
  57. public HubConnection(string url, IDictionary<string, string> queryString)
  58. : this(url, queryString, useDefaultUrl: true)
  59. {
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="HubConnection"/> class.
  63. /// </summary>
  64. /// <param name="url">The url to connect to.</param>
  65. /// <param name="queryString">The query string data to pass to the server.</param>
  66. /// <param name="useDefaultUrl">Determines if the default "/signalr" path should be appended to the specified url.</param>
  67. public HubConnection(string url, IDictionary<string, string> queryString, bool useDefaultUrl)
  68. : base(GetUrl(url, useDefaultUrl), queryString)
  69. {
  70. }
  71. protected override void OnReceived(JToken message)
  72. {
  73. var invocation = message.ToObject<HubInvocation>();
  74. HubProxy hubProxy;
  75. if (_hubs.TryGetValue(invocation.Hub, out hubProxy))
  76. {
  77. if (invocation.State != null)
  78. {
  79. foreach (var state in invocation.State)
  80. {
  81. hubProxy[state.Key] = state.Value;
  82. }
  83. }
  84. hubProxy.InvokeEvent(invocation.Method, invocation.Args);
  85. }
  86. base.OnReceived(message);
  87. }
  88. protected override string OnSending()
  89. {
  90. var data = _hubs.Select(p => new HubRegistrationData
  91. {
  92. Name = p.Key
  93. });
  94. return JsonConvert.SerializeObject(data);
  95. }
  96. /// <summary>
  97. /// Creates an <see cref="IHubProxy"/> for the hub with the specified name.
  98. /// </summary>
  99. /// <param name="hubName">The name of the hub.</param>
  100. /// <returns>A <see cref="IHubProxy"/></returns>
  101. public IHubProxy CreateProxy(string hubName)
  102. {
  103. if (State != ConnectionState.Disconnected)
  104. {
  105. throw new InvalidOperationException("Proxies cannot be added after the connection has been started.");
  106. }
  107. HubProxy hubProxy;
  108. if (!_hubs.TryGetValue(hubName, out hubProxy))
  109. {
  110. hubProxy = new HubProxy(this, hubName);
  111. _hubs[hubName] = hubProxy;
  112. }
  113. return hubProxy;
  114. }
  115. private static string GetUrl(string url, bool useDefaultUrl)
  116. {
  117. if (!url.EndsWith("/"))
  118. {
  119. url += "/";
  120. }
  121. if (useDefaultUrl)
  122. {
  123. return url + "signalr";
  124. }
  125. return url;
  126. }
  127. }
  128. }