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
我想处理数据流,需要实时显示处理后的数据。为此,我创建了一个hub类
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. And thus execution comes into ProcessEventsAsync in SimpleEventProcessor. From this ProcessEventsAsync method, I need to call the clientside code to display the data. Do I need to make SimpleEventProcessor also as a hub class?
在这里,我在Hub类中调用ProcessEvents方法并注册SimpleEventProcessor。因此,在SimpleEventProcessor中执行进入ProcessEventsAsync。通过这个ProcessEventsAsync方法,我需要调用客户端代码来显示数据。我是否需要将SimpleEventProcessor也作为hub类?
2 个解决方案
#1
2
You can use following code to get hold of HubContext which allows you to invoke client methods from outside of hub istance:
您可以使用以下代码获得HubContext,它允许您从hub istance外部调用客户端方法:
var hubContext = GlobalHost.ConnectionManager.GetHubContext<AzureGuidanceEventHubReceiver>();
hubContext.Clients.All.displayMessage(dataToDisplay);
#2
1
Here I am facing a problem that The client method DisplayMessage is not executing everytime. 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.
这里我遇到了一个问题,客户机方法DisplayMessage并不是每次都执行。我需要显示消息流。但是当我在客户机代码中放入调试器时,它每次都执行。这是我的客户代码。
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?
如何显示消息流?为什么它只使用调试器?
#1
2
You can use following code to get hold of HubContext which allows you to invoke client methods from outside of hub istance:
您可以使用以下代码获得HubContext,它允许您从hub istance外部调用客户端方法:
var hubContext = GlobalHost.ConnectionManager.GetHubContext<AzureGuidanceEventHubReceiver>();
hubContext.Clients.All.displayMessage(dataToDisplay);
#2
1
Here I am facing a problem that The client method DisplayMessage is not executing everytime. 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.
这里我遇到了一个问题,客户机方法DisplayMessage并不是每次都执行。我需要显示消息流。但是当我在客户机代码中放入调试器时,它每次都执行。这是我的客户代码。
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?
如何显示消息流?为什么它只使用调试器?