简体   繁体   English

如何编写两个具有服务器方法的集线器,它们在while循环中运行并将数据发送到SignalR客户端

[英]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.

相关问题 如何将数据发送回 SignalR Hub - How to send data back into SignalR Hub 如何使用Winform中的SignalR Hub将服务器上数据网格中存储的详细信息发送到客户端 - How to send details stored in datagrid on server to client using SignalR Hub in winform 从SignalR中的客户端调用Hub类之外的服务器方法 - Calling a server method that is outside of the Hub class from the client in SignalR SignalR - 用于检测客户端是否与集线器断开连接的服务器端方法? - SignalR - Server side method to detect if a client disconnects from a hub? 如何使用SignalR将文本框中的文本从客户端发送到集线器 - How to send text from textbox with SignalR from client to hub 如何从websocket-sharp客户端发送文件到SignalR集线器? - How to Send File to SignalR hub From websocket-sharp Client? signalR调用服务器方法,该方法调用集线器外部的回调方法。 如何从该方法调用客户端功能? - signalR calls a server method and that method calls a callback method ,outside the hub. How can I call client function from that method? SignalR - 如何从服务器上调用服务器上的Hub方法 - SignalR - How can I call Hub Method on server from server SignalR解析调用集线器方法的客户端 - SignalR resolve client that called hub method SignalR Java客户端无法调用方法并发送数据 - SignalR java client can't invoke method and send data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM