Looking for some assistance here. I have been chopping away for over a week now and seem to go in circles. I can get this to work in a none unity app no problem.
setup:
unity 2018.1
VS 2017
Hololens
Azure account(setup to receive and transmit realtime data)
Steps:
- Create Hololens Unity app per the Microsoft instructions : Install the tools - Mixed Reality | Microsoft Learn
- setup a simple scene with basic holographic capabilities + internet/client/server compatibility.
- copy the Azure sample code from : azure-event-hubs/samples/DotNet/Microsoft.Azure.EventHubs/SampleEphReceiver at master · Azure/azure-event-hubs · GitHub
- Translate it to be Unity acceptable
- build in to App folder and open UWP project.
- build for hololens and see that while the scene appears that after a short time the async fails and tosses an error.
So here is what I think is happening.
- I call the Async function from Start().
- This creates a new EventProcessorHost
- inside the async is a Try/Catch that runs the SimpleEventProcessor script
- I change the text readout in immersive view to show " TEMPERATURE: " as a place holder
- The Try does calls simpleeventprocessor but only the Open and Close functions. I get no data.
- Scratch my head and try again
The App deploys fine to Hololens and I can see in immersive view that the “Receiver” script is working as it updates the UI TextMesh, however I don’t get the data from eventhub.
I used the same code in a VS console app and I retrieve the data correctly.
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
#if UNITY_UWP
using Microsoft.Azure.EventHubs.Processor;
#endif
using UnityEngine;
public class Receiver : MonoBehaviour
{
private const string EventHubConnectionString = "XXXXXX";
private const string EventHubName = "XXXXXX";
private const string StorageContainerName = "XXXXXX";
private const string StorageAccountName = "XXXXXX";
private const string StorageAccountKey = "XXXXXXX";
private static readonly string StorageConnectionString = string.Format("XXXXXX");
public static TextMesh textMesh;
public static string data;
public async void Start()
{
textMesh = gameObject.GetComponent<TextMesh>();
textMesh.text = "Temp: " ;
textMesh.fontSize = 18;
//await MainAsync();
await RunTheMethod();
}
public async Task RunTheMethod()
{
while (true)
{
await MainAsync();
}
}
public static async Task MainAsync()
{
#if UNITY_UWP
var eventProcessorHost = new EventProcessorHost(
EventHubName,
PartitionReceiver.DefaultConsumerGroupName,
EventHubConnectionString,
StorageConnectionString,
StorageContainerName);
try
{
textMesh.text += "\ntry: ";
// Registers the Event Processor Host and starts receiving messages
await eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>();
data = SimpleEventProcessor.tempData;
textMesh.text += "\nTemperature: " + data;
}
catch
{
textMesh.text = "ERROR: ";
}
//textMesh.text += "\n Outof TRy/Catch: " + data;
// Disposes of the Event Processor Host
await eventProcessorHost.UnregisterEventProcessorAsync();
data = SimpleEventProcessor.tempData;
textMesh.text += "\nreturned to receiver: " + data.ToString();
#endif
}
#if UNITY_UWP
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;
using Microsoft.Azure.EventHubs.Processor;
using UnityEngine;
public class SimpleEventProcessor : IEventProcessor
{
public static string tempData;
public Task CloseAsync(PartitionContext context, CloseReason reason)
{
return Task.CompletedTask;
}
public Task OpenAsync(PartitionContext context)
{
tempData += "\n OpenAsync";
return Task.CompletedTask;
}
public Task ProcessErrorAsync(PartitionContext context, Exception error)
{
tempData += "\nprocessError";
return Task.CompletedTask;
}
public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
tempData += "\n processing";
foreach (var eventData in messages)
{
var data = Encoding.UTF8.GetString(eventData.Body.Array, eventData.Body.Offset, eventData.Body.Count);
tempData += "\n SUCCESS" ;
}
return context.CheckpointAsync();
}
}
#endif