Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added ability to override hub url in .NET Client.

Fixes #368.
  • Loading branch information...
commit c303e480354c227fa83d84522ffe413e271ab735 1 parent 44ff03a
@davidfowl davidfowl authored
View
49 SignalR.Client/Hubs/HubConnection.cs
@@ -8,12 +8,49 @@
namespace SignalR.Client.Hubs
{
+ /// <summary>
+ /// A <see cref="Connection"/> for interacting with Hubs.
+ /// </summary>
public class HubConnection : Connection
{
private readonly Dictionary<string, HubProxy> _hubs = new Dictionary<string, HubProxy>(StringComparer.OrdinalIgnoreCase);
+ /// <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)
- : base(GetUrl(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)
+ : base(url, 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)
+ : base(url, queryString)
{
}
@@ -75,13 +112,19 @@ private string OnConnectionSending()
return JsonConvert.SerializeObject(data);
}
- private static string GetUrl(string url)
+ private static string GetUrl(string url, bool useDefaultUrl)
{
if (!url.EndsWith("/"))
{
url += "/";
}
- return url + "signalr";
+
+ if (useDefaultUrl)
+ {
+ return url + "signalr";
+ }
+
+ return url;
}
}
}
View
26 SignalR.Tests/HubFacts.cs
@@ -1,4 +1,6 @@
using System;
+using System.Threading;
+using SignalR.Client.Hubs;
using SignalR.Hosting.Memory;
using Xunit;
@@ -88,5 +90,29 @@ public void Overloads()
Assert.Equal(1, n);
}
+
+ [Fact]
+ public void ChangeHubUrl()
+ {
+ var host = new MemoryHost();
+ host.MapHubs("/foo");
+ var connection = new Client.Hubs.HubConnection("http://site/foo", useDefaultUrl: false);
+
+ var hub = connection.CreateProxy("demo");
+
+ var wh = new ManualResetEvent(false);
+
+ hub.On("signal", id =>
+ {
+ Assert.NotNull(id);
+ wh.Set();
+ });
+
+ connection.Start(host).Wait();
+
+ hub.Invoke("DynamicTask").Wait();
+
+ Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
+ }
}
}
View
8 SignalR.Tests/HubProxyTest.cs
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
-using System.Linq;
+using System.Threading;
using System.Threading.Tasks;
using Moq;
using SignalR.Client.Hubs;
@@ -112,19 +112,19 @@ public void EndToEndTest()
var hubConnection = new HubConnection("http://fake");
IHubProxy proxy = hubConnection.CreateProxy("ChatHub");
- var called = false;
+ var wh = new ManualResetEvent(false);
proxy.On("addMessage", data =>
{
- called = true;
Assert.Equal("hello", data);
+ wh.Set();
});
hubConnection.Start(host).Wait();
proxy.Invoke("Send", "hello").Wait();
- Assert.True(called);
+ Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
}
[Fact]
Please sign in to comment.
Something went wrong with that request. Please try again.