简体   繁体   中英

how to write two hub which has server method runs in while loop and send data to SignalR client

I have requirement to show live stock data and at the same time notification on the same page.I have used angular js,dot net signalR and put two while loop which will listen to streamer and push data to client but its not giving expected result.

//First Client

var stockFeedHub = $.connection.stockFeedHub;
    $.connection.hub.start();
    $.connection.hub.logging = true;
    $.connection.hub.start().done(function () {
        stockFeedHub.server.send("-1");
    })
    .fail(function () { console.log('Could not connect stockFeedHub'); });

    stockFeedHub.client.sendStocksData = function (data) {
}

//Second Client

var notificationFeedHub = $.connection.notificationFeedHub;
    $.connection.hub.start().done(function () {
        notificationFeedHub.server.getNotification();
    })
    .fail(function () { console.log("fail"); });

    notificationFeedHub.client.sendNotification = function (data) {
//some logic
    }

//Hub one-

 public class NotificationFeedHub : Hub
    {
        public void GetNotification()
        {
                while (true)
                {
                    //NotificationMessage I am getting from Redis.
                    //Call Client method to send notification.

                    Clients.All.sendNotification(notificationMessage));
                    Thread.Sleep(1000*5);
                }
        }

    }

//2nd Hub class

public class StockFeedHub: Hub
        {
            public void Send()
            {
                    while (true)
                    {
                        //I am getting stockLiveData from streamer server.
                        //Call Client method to send sendStocksData.

                        Clients.All.sendStocksData(stockLiveData));
                        Thread.Sleep(1000*5);
                    }
            }

        }

I am facing issue due to while loop and i am not sure what else i can do if i want to get both data continuously.

You're currently using the web clients to trigger the push, then stealing the request thread each time the request is made and blocking it with a sleep. Don't steal SignalR's threads, rather create your own thread or use one that is already running and there for you.

In this case your stock streamer should have its own thread that is raising stock update events. If so call your StockFeedHub and dispatch your stock updates to all clients when you receive a new stock update.

void HandleNewStockMessage(StockModel stock){
  GlobalHost
    .ConnectionManager
    .GetHubContext<StockFeedHub>()
    .Clients.All.sendStocksData(stock);
}

More information on Signalr calling hubs .

I'm not sure how you're using Redis I guess you're using it for your backplane. If so then check the scaleout with redis document. You should only need to dispatch your notifications (similar to your stock data) when you receive them and your backplane will scale the messages out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM