PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Microsoft.AspNet.SignalR.Client/Hubs/HubConnection.cs

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