使用Azure事件中心/ IoT中心SDK来在Unity中修复“无效URI”吗?

时间:2019-05-07 10:27:29

标签: unity3d azure-iot-hub azure-eventhub

我正在尝试将数据从Azure IoT中心接收到我的Unity应用。我面临有关URI的问题。

首先,全局图片是将温度数据发送到Azure IoT中心的树莓派(感谢Azure设备IoT中心Python SDK)。借助Azure Event Hub.NET SDK,我的Unity可以连接到Azure IoT中心的Event中心端点以接收温度数据。

我修改了Azure提供的c#示例代码,使其与Unity一起使用。但是,当我播放该应用程序时,我收到一条错误消息“ Invalid Uri”。

错误消息: UriFormatException:无效的URI:URI方案无效。 System.Uri.CreateThis(System.String uri,System.Boolean dontEscape,System.UriKind uriKind)(位于:0) System.Uri..ctor(System.String uriString)(位于:0) deviceToCloud + d__6.MoveNext()(在Assets / Scripts / deviceToCloud.cs:89) 重新抛出为AggregateException:发生一个或多个错误。 System.Threading.Tasks.Task.ThrowIfExceptional(System.Boolean includeTaskCanceledExceptions)(在<1f0c1ef1ad524c38bbc5536809c46b48>:0) System.Threading.Tasks.Task.Wait(System.Int32毫秒超时,System.Threading.CancellationToken cancelledToken)(位于<1f0c1ef1ad524c38bbc5536809c46b48>:0) System.Threading.Tasks.Task.Wait()(位于<1f0c1ef1ad524c38bbc5536809c46b48>:0) deviceToCloud.Start()(位于Assets / Scripts / deviceToCloud.cs:121)

有人对这个错误及其解决方法有想法吗? 这是关于端点的吗?

感谢您的帮助!

这是代码。



public class deviceToCloud : MonoBehaviour

{
    // Event Hub-compatible endpoint
    private readonly static string s_eventHubsCompatibleEndpoint = "Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=xxx;SharedAccessKey=xxx;EntityPath=xxx";

    // Event Hub-compatible name
    private readonly static string s_eventHubsCompatiblePath = "xxx";

    // Keys
    private readonly static string s_iotHubSasKey = "xxx";
    private readonly static string s_iotHubSasKeyName = "xxx";
    private static EventHubClient s_eventHubClient;

    // Asynchronously create a PartitionReceiver for a partition and then start 
    // reading any messages sent from the simulated client.
    private static async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct)
    {

        // Create the receiver using the default consumer group.
        // For the purposes of this sample, read only messages sent since 
        // the time the receiver is created. Typically, you don't want to skip any messages.
        var eventHubReceiver = s_eventHubClient.CreateReceiver("$Default", partition, EventPosition.FromEnqueuedTime(DateTime.Now));

        while (true)
        {
            if (ct.IsCancellationRequested) break;
            // Check for EventData - this methods times out if there is nothing to retrieve.
            var events = await eventHubReceiver.ReceiveAsync(100);

            // If there is data in the batch, process it.
            if (events == null) continue;

            foreach (EventData eventData in events)
            {
                string data = Encoding.UTF8.GetString(eventData.Body.Array);
                Debug.Log("Message received: " + data);
            }

        }

    }

    //private static async Task Main(string[] args)
    public  async Task ReadD2C()

    {
        Debug.Log("Fonction principale ReadD2C");

        // Create an EventHubClient instance to connect to the
        // IoT Hub Event Hubs-compatible endpoint.
        var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompatibleEndpoint), s_eventHubsCompatiblePath, s_iotHubSasKeyName, s_iotHubSasKey);
        s_eventHubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

        // Create a PartitionReciever for each partition on the hub.
        var runtimeInfo = await s_eventHubClient.GetRuntimeInformationAsync();
        var d2cPartitions = runtimeInfo.PartitionIds;

        CancellationTokenSource cts = new CancellationTokenSource();

        Console.CancelKeyPress += (s, e) =>
        {
            e.Cancel = true;
            cts.Cancel();
            Debug.Log("Exit");
        };

        var tasks = new List<Task>();
        foreach (string partition in d2cPartitions)
        {
            tasks.Add(ReceiveMessagesFromDeviceAsync(partition, cts.Token));
        }

        // Wait for all the PartitionReceivers to finsih.
        Task.WaitAll(tasks.ToArray());
    }

    private void Start()
    {
        Debug.Log("Start");

        ReadD2C().Wait();

    }
}

1 个答案:

答案 0 :(得分:0)

似乎s_eventHubsCompatibleEndpoint不是正确的地址。也许像这样尝试呢?

sb://xxx.servicebus.windows.net/;SharedAccessKeyName=xxx;SharedAccessKey=xxx;EntityPath=xxx

您确定这也是正确的网址吗?