public class StaticContext
{
private readonly IHubConnectionContext<dynamic> _clients;
public static StaticContext Instance { get { return _instance.Value; } }
// Singleton instance
private readonly static Lazy<StaticContext> _instance = new Lazy<StaticContext>(
() => new
StaticContext(GlobalHost.ConnectionManager.GetHubContext<HubWithStaticContext>().Clients));
private StaticContext(IHubConnectionContext<dynamic> clients)
{
// Clients From Hub
_clients = clients;
}
public void Send(string name, string message)
{
_clients.All.addMessage(name, message);
}
public void DoSomething()
{
_clients.All.addMessage("I am", "working now");
}
}
public class HubWithStaticContext : Hub
{
private readonly StaticContext _staticContext;
public HubWithStaticContext() : this(StaticContext.Instance) { }
public HubWithStaticContext(StaticContext staticContext)
{
_staticContext = staticContext;
}
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
public void DoSomethingInStaticContext()
{
_staticContrext.DoSomething();
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Cross Domain
app.UseCors(CorsOptions.AllowAll);
// Map SignalR
app.MapSignalR();
}
}
static void Main(string[] args)
{
var url = "http://localhost:8081";
Log("Try to start Server", url);
using (WebApp.Start<Startup>(url))
{
// Console.WriteLine("Server running on {0}", url);
Log("Server running on", url);
Console.ReadLine();
}
}
private static void Log(string subject, string message)
{
Console.WriteLine("{0} {1} {2}", DateTime.Now.TimeOfDay.ToString("T"), subject, message);
}
Комментариев нет:
Отправить комментарий