[英]signalR calls a server method and that method calls a callback method ,outside the hub. How can I call client function from that method?
I want to process a stream of data and need to display the processed data near real time. 我想处理数据流,需要实时显示处理过的数据。 For that I created a hub class
为此,我创建了一个集线器类
public class AzureGuidanceEventHubReceiver : Hub
{
EventProcessorHost eventProcessorHost;
public async void ProcessEvents()
{
//Do some code here
eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();
}
}
And the class which processes the data is, 处理数据的类是,
public class SimpleEventProcessor : IEventProcessor
{
public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> events)
{
foreach (EventData eventData in events)
{
int data;
var newData = this.DeserializeEventData(eventData);
//how to display newData in the browser????????????????????????????????
}
}
My client side code is 我的客户端代码是
<script type="text/javascript">
$(function () {
var receiverHub= $.connection.azureGuidanceEventHubReceiver;
receiverHub.client.displayMessage = function (data) {
var encodedData = $('<div />').text(data).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedData + '</li>');
};
//// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
receiverHub.server.processEvents();
});
});
});
Here I made a call to ProcessEvents method in the Hub class and that registers the SimpleEventProcessor. 在这里,我调用了Hub类中的ProcessEvents方法,并注册了SimpleEventProcessor。 And thus execution comes into ProcessEventsAsync in SimpleEventProcessor.
因此执行进入SimpleEventProcessor中的ProcessEventsAsync。 From this ProcessEventsAsync method, I need to call the clientside code to display the data.
从这个ProcessEventsAsync方法,我需要调用客户端代码来显示数据。 Do I need to make SimpleEventProcessor also as a hub class?
我是否还需要将SimpleEventProcessor作为集线器类?
You can use following code to get hold of HubContext which allows you to invoke client methods from outside of hub istance: 您可以使用以下代码来获取HubContext,它允许您从集线器以外的地方调用客户端方法:
var hubContext = GlobalHost.ConnectionManager.GetHubContext<AzureGuidanceEventHubReceiver>();
hubContext.Clients.All.displayMessage(dataToDisplay);
Here I am facing a problem that The client method DisplayMessage is not executing everytime. 在这里,我遇到的问题是客户端方法DisplayMessage不是每次都执行。 I need to display a stream of message.
我需要显示一条消息流。 But when I put debugger in the client code, it executes everytime.Here is my client code.
但是当我将调试器放在客户端代码中时,它每次都会执行。这是我的客户端代码。
chat.client.displayMessage = function (data) {
// Html encode display data
debugger;
var encodedData = $('<div />').text(data.GPSPosition).html();
var data = encodedData.split("\n");
var varlat = data[0].replace("Latitude:","").trim();
var varlong = data[1].replace("Longitude:", "").trim();
ShowInGoogleMap(varlat, varlong);
};
how can i display a stream of messages? 我该如何显示消息流? Why it is only working with debugger?
为什么它只与调试器一起使用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.