I have a self hosted signalr hub, configured like this:
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
namespace SignalRSelfHost
{
class Program
{
static void Main(string[] args)
{
// This will *ONLY* bind to localhost, if you want to bind to all addresses
// use http://*:8080 to bind to all addresses.
// See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx
// for more information.
string url = "http://localhost:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class MyHub : Hub
{
ConcurrentDictionary<string, string[]> userList = new ConcurrentDictionary<string, string[]>();
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
}
I have some process which will be running "forever"
CancellationTokenSource cts = new CancellationTokenSource();
var token = cts.Token;
Task.Factory.StartNew(() =>
{
while (true)
{
//update the userList which should be shared with hub, and call some signalr notification on hub clients
Thread.Sleep(1000);
}
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
I tried to put this process in the hub constructor, but it didn't work well.
So, how to achieve this?