Skip to content

Commit

Permalink
Fixed Auto Transport and SSE
Browse files Browse the repository at this point in the history
  • Loading branch information
NTaylorMullen committed Oct 9, 2012
1 parent 0b57369 commit 18f3d91
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
13 changes: 10 additions & 3 deletions SignalR.Client/Connection.cs
Expand Up @@ -201,7 +201,7 @@ public virtual Task Start(IClientTransport transport)
}

_transport = transport;

Task negotation = Negotiate(transport);

negotation.ContinueWith(task =>
Expand All @@ -212,7 +212,7 @@ public virtual Task Start(IClientTransport transport)
_transport.MonitorKeepAlive(this);
}
});


return negotation;
}
Expand All @@ -228,10 +228,17 @@ private Task Negotiate(IClientTransport transport)

transport.Negotiate(this).Then(negotiationResponse =>
{
var keepAlive = negotiationResponse.KeepAlive;
VerifyProtocolVersion(negotiationResponse.ProtocolVersion);
ConnectionId = negotiationResponse.ConnectionId;
if (keepAlive.HasValue)
{
transport.RegisterKeepAlive(TimeSpan.FromSeconds(keepAlive.Value));
}
var data = OnSending();
StartTransport(data).ContinueWith(negotiateTcs);
})
Expand Down Expand Up @@ -475,6 +482,6 @@ private static bool TryParseVersion(string versionString, out Version version)
private static string CreateQueryString(IDictionary<string, string> queryString)
{
return String.Join("&", queryString.Select(kvp => kvp.Key + "=" + kvp.Value).ToArray());
}
}
}
}
15 changes: 14 additions & 1 deletion SignalR.Client/Transports/AutoTransport.cs
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using SignalR.Client.Http;

Expand All @@ -14,6 +15,8 @@ public class AutoTransport : IClientTransport
// List of transports in fallback order
private readonly IClientTransport[] _transports;

private TimeSpan? _keepAliveToRegister;

public AutoTransport(IHttpClient httpClient)
{
_httpClient = httpClient;
Expand Down Expand Up @@ -73,6 +76,11 @@ private void ResolveTransport(IConnection connection, string data, TaskCompletio
// Set the active transport
_transport = transport;
if (_keepAliveToRegister.HasValue)
{
_transport.RegisterKeepAlive(_keepAliveToRegister.Value);
}
// Complete the process
tcs.SetResult(null);
}
Expand All @@ -93,6 +101,11 @@ public void Stop(IConnection connection, bool notifyServer = true)
}
}

public void RegisterKeepAlive(TimeSpan keepAlive)
{
_keepAliveToRegister = keepAlive;
}

public bool SupportsKeepAlive()
{
if (_transport != null)
Expand Down
23 changes: 8 additions & 15 deletions SignalR.Client/Transports/HttpBasedTransport.cs
Expand Up @@ -45,21 +45,7 @@ public HttpBasedTransport(IHttpClient httpClient, string transport)

public Task<NegotiationResponse> Negotiate(IConnection connection)
{
var negotationData = GetNegotiationResponse(_httpClient, connection);

if (negotationData.Status != TaskStatus.Faulted)
{
var keepAlive = negotationData.Result.KeepAlive;

if (keepAlive.HasValue)
{
_supportsKeepAlive = true;
// Setting the keep alive will calculate the monitoring thresholds
_keepAliveData.KeepAlive = TimeSpan.FromSeconds(keepAlive.Value);
}
}

return negotationData;
return GetNegotiationResponse(_httpClient, connection);
}

internal static Task<NegotiationResponse> GetNegotiationResponse(IHttpClient httpClient, IConnection connection)
Expand Down Expand Up @@ -362,6 +348,13 @@ private static string GetCustomQueryString(IConnection connection)
: "&" + connection.QueryString;
}

public void RegisterKeepAlive(TimeSpan keepAlive)
{
_supportsKeepAlive = true;
// Setting the keep alive will calculate the monitoring thresholds
_keepAliveData.KeepAlive = keepAlive;
}

public bool SupportsKeepAlive()
{
return _supportsKeepAlive;
Expand Down
2 changes: 2 additions & 0 deletions SignalR.Client/Transports/IClientTransport.cs
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using System.Threading;
using System;

namespace SignalR.Client.Transports
{
Expand All @@ -10,6 +11,7 @@ public interface IClientTransport
Task<T> Send<T>(IConnection connection, string data);
void Stop(IConnection connection, bool notifyServer = true);

void RegisterKeepAlive(TimeSpan keepAlive);
void LostConnection(IConnection connection);
bool SupportsKeepAlive();
void MonitorKeepAlive(IConnection connection);
Expand Down
2 changes: 1 addition & 1 deletion SignalR.Client/Transports/ServerSentEventsTransport.cs
Expand Up @@ -44,7 +44,7 @@ protected override void OnStart(IConnection connection, string data, Action init
public override void LostConnection(IConnection connection)
{
// Stopping the transport will force it into auto reconnect and maintain the functional flow
Stop(connection);
Stop(connection, false);
}

private void Reconnect(IConnection connection, string data)
Expand Down

0 comments on commit 18f3d91

Please sign in to comment.