Skip to content

Instantly share code, notes, and snippets.

@trbngr
Created October 3, 2012 16:16
Show Gist options
  • Select an option

  • Save trbngr/3827957 to your computer and use it in GitHub Desktop.

Select an option

Save trbngr/3827957 to your computer and use it in GitHub Desktop.
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