3

如果我通过 IoT-Hub 发送消息(Cloud 2 Device):

var serviceMessage= new Message(Encoding.ASCII.GetBytes("Hello Device"));
serviceMessage.Ack = DeliveryAcknowledgement.Full;
commandMessage.MessageId = Guid.NewGuid().ToString();
await serviceClient.SendAsync("myDeviceID", serviceMessage);  //Send message here

并尝试接收来自客户端的确认:

bool feedbackReceived = false;
while(!feedbackReceived){
  FeedbackReceiver<FeedbackBatch> feedbackReceiver = serviceClient.GetFeedbackReceiver();
  var feedbackBatch = await feedbackReceiver.ReceiveAsync(TimeSpan.FromSeconds(1));
  if(feedbackBatch != null)
    {
      feedbackReceived = feedbackBatch.Records.Any(fm => fm.OriginalMessageId == serviceMessage.MessageId);
      if (feedbackReceived)
        {
          await feedbackReceiver.CompleteAsync(feedbackBatch);
          feedbackReceiver = null;
        }
    }
}

我的客户立即收到消息并发送反馈:

DeviceClient deviceClient = DeviceClient.Create(iotHubUri, new DeviceAuthenticationWithRegistrySymmetricKey(bridgeID, deviceKey), TransportType.Amqp);
Message receivedMessage = await deviceClient.ReceiveAsync();
await deviceClient.CompleteAsync(receivedMessage);

我的云收到反馈最多需要 15 秒。如果我在循环中发送消息,那么第一条消息需要 1 到 15 秒之间的时间,并且每个后续响应都需要 15 秒。

为什么需要这么长时间?我可以改变它吗?我云中的接收方法立即得到答案:

var incommingMessage = eventHubReceiver.ReceiveAsync();
incommingMessage.Wait();

如果客户端发送消息:

var message = new Message(Encoding.ASCII.GetBytes("My Message"));
await deviceClient.SendEventAsync(message);

有问题的整个项目都在 gitHub 上: https ://github.com/Ben4485/Azure_IotHub_Get_Response

4

1 回答 1

3

当然,15 秒很多。但是,反馈不是单个消息,而是包含来自更多设备的更多反馈的批次(带有反馈数组的 JSON 文档)。在将反馈发送到系统之前,系统可能会尝试尽可能多地获取反馈。

保罗。

于 2015-11-13T11:30:19.387 回答