[英]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. 我需要在同一页面上显示实时库存数据并同时进行通知。我使用了角度js,点网signalR并放置了两个while循环,该循环将侦听流媒体并将数据推送到客户端,但未给出预期结果。
//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 //第二个Hub类
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. 我由于while循环而面临问题,如果我想连续获取两个数据,我不确定还能做什么。
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. 当前,您正在使用Web客户端触发推送,然后在每次发出请求时都窃取请求线程,并在睡眠状态下阻止它。 Don't steal SignalR's threads, rather create your own thread or use one that is already running and there for you.
不要窃取SignalR的线程,而要创建您自己的线程或使用已经运行的线程为您服务。
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.
如果是这样,请在收到新的库存更新时致电StockFeedHub并将库存更新发送给所有客户。
void HandleNewStockMessage(StockModel stock){
GlobalHost
.ConnectionManager
.GetHubContext<StockFeedHub>()
.Clients.All.sendStocksData(stock);
}
More information on Signalr calling hubs . 有关Signalr 呼叫中心的更多信息。
I'm not sure how you're using Redis I guess you're using it for your backplane. 我不确定您如何使用Redis,我想您是将其用于背板。 If so then check the scaleout with redis document.
如果是这样,请使用redis文档检查横向扩展 。 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.
收到通知时,您只需要发送通知(类似于库存数据),并且背板将向外扩展消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.