如果我通过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
答案 0 :(得分:3)
当然15秒很多。但是,反馈不是单个消息,而是总是批处理(具有反馈数组的JSON文档),其中包含来自更多设备的更多反馈。在将系统发送到系统之前,系统可能会尝试获取更多反馈。
保罗。