forked from oising/Nivot.SignalR.Client.Net35
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHubConnection.cs
More file actions
232 lines (204 loc) · 7.87 KB
/
Copy pathHubConnection.cs
File metadata and controls
232 lines (204 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client.Hubs;
using Newtonsoft.Json.Linq;
namespace Microsoft.AspNet.SignalR.Client
{
/// <summary>
/// A <see cref="Connection"/> for interacting with Hubs.
/// </summary>
public class HubConnection : Connection, IHubConnection
{
private readonly Dictionary<string, HubProxy> _hubs = new Dictionary<string, HubProxy>(StringComparer.OrdinalIgnoreCase);
internal readonly Dictionary<string, Action<HubResult>> _callbacks = new Dictionary<string, Action<HubResult>>();
private int _callbackId;
/// <summary>
/// Initializes a new instance of the <see cref="HubConnection"/> class.
/// </summary>
/// <param name="url">The url to connect to.</param>
public HubConnection(string url)
: this(url, useDefaultUrl: true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HubConnection"/> class.
/// </summary>
/// <param name="url">The url to connect to.</param>
/// <param name="useDefaultUrl">Determines if the default "/signalr" path should be appended to the specified url.</param>
public HubConnection(string url, bool useDefaultUrl)
: base(GetUrl(url, useDefaultUrl))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HubConnection"/> class.
/// </summary>
/// <param name="url">The url to connect to.</param>
/// <param name="queryString">The query string data to pass to the server.</param>
public HubConnection(string url, string queryString)
: this(url, queryString, useDefaultUrl: true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HubConnection"/> class.
/// </summary>
/// <param name="url">The url to connect to.</param>
/// <param name="queryString">The query string data to pass to the server.</param>
/// <param name="useDefaultUrl">Determines if the default "/signalr" path should be appended to the specified url.</param>
public HubConnection(string url, string queryString, bool useDefaultUrl)
: base(GetUrl(url, useDefaultUrl), queryString)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HubConnection"/> class.
/// </summary>
/// <param name="url">The url to connect to.</param>
/// <param name="queryString">The query string data to pass to the server.</param>
public HubConnection(string url, IDictionary<string, string> queryString)
: this(url, queryString, useDefaultUrl: true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HubConnection"/> class.
/// </summary>
/// <param name="url">The url to connect to.</param>
/// <param name="queryString">The query string data to pass to the server.</param>
/// <param name="useDefaultUrl">Determines if the default "/signalr" path should be appended to the specified url.</param>
public HubConnection(string url, IDictionary<string, string> queryString, bool useDefaultUrl)
: base(GetUrl(url, useDefaultUrl), queryString)
{
}
public override void OnReconnecting()
{
ClearInvocationCallbacks(Resources.Message_Reconnecting);
base.OnReconnecting();
}
[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "")]
protected override void OnMessageReceived(JToken message)
{
if (message["I"] != null)
{
var result = message.ToObject<HubResult>(JsonSerializer);
Action<HubResult> callback;
lock (_callbacks)
{
if (_callbacks.TryGetValue(result.Id, out callback))
{
_callbacks.Remove(result.Id);
}
else
{
Trace(TraceLevels.Messages, "Callback with id " + result.Id + " not found!");
}
}
if (callback != null)
{
callback(result);
}
}
else
{
var invocation = message.ToObject<HubInvocation>(JsonSerializer);
HubProxy hubProxy;
if (_hubs.TryGetValue(invocation.Hub, out hubProxy))
{
if (invocation.State != null)
{
foreach (var state in invocation.State)
{
hubProxy[state.Key] = state.Value;
}
}
hubProxy.InvokeEvent(invocation.Method, invocation.Args);
}
base.OnMessageReceived(message);
}
}
protected override string OnSending()
{
var data = _hubs.Select(p => new HubRegistrationData
{
Name = p.Key
});
return this.JsonSerializeObject(data);
}
protected override void OnClosed()
{
ClearInvocationCallbacks(Resources.Message_ConnectionClosed);
base.OnClosed();
}
/// <summary>
/// Creates an <see cref="IHubProxy"/> for the hub with the specified name.
/// </summary>
/// <param name="hubName">The name of the hub.</param>
/// <returns>A <see cref="IHubProxy"/></returns>
public IHubProxy CreateHubProxy(string hubName)
{
if (State != ConnectionState.Disconnected)
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_ProxiesCannotBeAddedConnectionStarted));
}
HubProxy hubProxy;
if (!_hubs.TryGetValue(hubName, out hubProxy))
{
hubProxy = new HubProxy(this, hubName);
_hubs[hubName] = hubProxy;
}
return hubProxy;
}
string IHubConnection.RegisterCallback(Action<HubResult> callback)
{
lock (_callbacks)
{
string id = _callbackId.ToString(CultureInfo.InvariantCulture);
_callbacks[id] = callback;
_callbackId++;
return id;
}
}
void IHubConnection.RemoveCallback(string callbackId)
{
lock (_callbacks)
{
_callbacks.Remove(callbackId);
}
}
private static string GetUrl(string url, bool useDefaultUrl)
{
if (!url.EndsWith("/", StringComparison.Ordinal))
{
url += "/";
}
if (useDefaultUrl)
{
return url + "signalr";
}
return url;
}
private void ClearInvocationCallbacks(string error)
{
Action<HubResult>[] callbacks;
lock (_callbacks)
{
callbacks = _callbacks.Values.ToArray();
_callbacks.Clear();
}
foreach (var callback in callbacks)
{
// Create a new HubResult each time as it's mutable and we don't want callbacks
// changing it during their parallel invocation
Task.Factory.StartNew(() => callback(new HubResult { Error = error }))
.Catch();
}
}
public void Force()
{
ClearInvocationCallbacks("Force");
}
}
}