0

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
iuristona
  • 927
  • 13
  • 30
  • I would question my self if polling really is the solution. In most cases a service bus / event aggregator, etc is what you want – Anders Aug 29 '14 at 11:57
  • I have this server wich by socket I just can request the list of users. – iuristona Aug 29 '14 at 12:03

1 Answers1

0

the Hub only lives during a SignalR request, so you should not have that kind of code in the Hub move it to a background worker and invoke the hub using

GlobalHost.ConnectionManager.GetHubContext<MyHub>().Clients.All.addMessage(name, message);
Anders
  • 17,306
  • 10
  • 76
  • 144