-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TransportHelper.cs
85 lines (69 loc) · 3.18 KB
/
TransportHelper.cs
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client.Http;
using Newtonsoft.Json;
using Microsoft.AspNet.SignalR.Client.Infrastructure;
namespace Microsoft.AspNet.SignalR.Client.Transports
{
public class TransportHelper
{
// virtual to allow mocking
public virtual Task<NegotiationResponse> GetNegotiationResponse(IHttpClient httpClient, IConnection connection, string connectionData)
{
if (httpClient == null)
{
throw new ArgumentNullException("httpClient");
}
if (connection == null)
{
throw new ArgumentNullException("connection");
}
var negotiateUrl = UrlBuilder.BuildNegotiate(connection, connectionData);
httpClient.Initialize(connection);
return httpClient.Get(negotiateUrl, connection.PrepareRequest, isLongRunning: false)
.Then(response => response.ReadAsString())
.Then(raw =>
{
if (String.IsNullOrEmpty(raw))
{
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.Error_ServerNegotiationFailed));
}
return JsonConvert.DeserializeObject<NegotiationResponse>(raw);
});
}
// virtual to allow mocking
public virtual Task<string> GetStartResponse(IHttpClient httpClient, IConnection connection, string connectionData, string transport)
{
if (httpClient == null)
{
throw new ArgumentNullException("httpClient");
}
if (connection == null)
{
throw new ArgumentNullException("connection");
}
var startUrl = UrlBuilder.BuildStart(connection, transport, connectionData);
return httpClient.Get(startUrl, connection.PrepareRequest, isLongRunning: false)
.Then(response => response.ReadAsString());
}
public static bool VerifyLastActive(IConnection connection)
{
if (connection == null)
{
throw new ArgumentNullException("connection");
}
// Ensure that we have not exceeded the reconnect window
if (DateTime.UtcNow - connection.LastActiveAt >= connection.ReconnectWindow)
{
connection.Trace(TraceLevels.Events, "There has not been an active server connection for an extended period of time. Stopping connection.");
connection.Stop(new TimeoutException(String.Format(CultureInfo.CurrentCulture, Resources.Error_ReconnectWindowTimeout,
connection.LastActiveAt, connection.ReconnectWindow)));
return false;
}
return true;
}
}
}