Created
October 3, 2012 16:16
-
-
Save trbngr/3827957 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| using System.Web.Http; | |
| using SignalR.Client; | |
| using SignalR.Client.Hubs; | |
| using SignalR.Hubs; | |
| namespace EventDay.Web.Api.Controllers | |
| { | |
| public abstract class ApiHubController<THub> : ApiController where THub : IHub | |
| { | |
| private readonly HubConnection _connection; | |
| private readonly Task<HubProxy> _proxyTask; | |
| protected ApiHubController() | |
| { | |
| var url = string.Format("http://app.{0}", Application.TopLevelDomain); | |
| _connection = new HubConnection(url); | |
| var proxy = new HubProxy(_connection, GetHubName()); | |
| _proxyTask = _connection.Start().ContinueWith(t => proxy); | |
| } | |
| protected IHubProxy Hub | |
| { | |
| get { return _proxyTask.Result; } | |
| } | |
| private string GetHubName() | |
| { | |
| Type type = typeof (THub); | |
| HubNameAttribute attribute = | |
| type.GetCustomAttributes(typeof (HubNameAttribute), false).Cast<HubNameAttribute>().SingleOrDefault(); | |
| return attribute != null ? attribute.HubName : type.Name; | |
| } | |
| protected override void Dispose(bool disposing) | |
| { | |
| if(disposing && _connection.State != ConnectionState.Disconnected) | |
| _connection.Stop(); | |
| base.Dispose(disposing); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment