選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

84 行
3.4 KiB

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using MQTTnet.Client;
  5. using MQTTnet.Extensions.Rpc;
  6. using MQTTnet.Protocol;
  7. namespace MQTTnet.Samples.RpcClient;
  8. public static class RcpClient_Samples
  9. {
  10. /*
  11. * The extension MQTTnet.Extensions.Rpc (available as nuget) allows sending a request and waiting for the matching reply.
  12. * This is done via defining a pattern which uses the topic to correlate the request and the response.
  13. * From client usage it is possible to define a timeout.
  14. */
  15. public static async Task Send_Request()
  16. {
  17. var mqttFactory = new MqttFactory();
  18. // The RPC client is an addon for the existing client. So we need a regular client
  19. // which is wrapped later.
  20. using (var mqttClient = mqttFactory.CreateMqttClient())
  21. {
  22. var mqttClientOptions = new MqttClientOptionsBuilder()
  23. .WithTcpServer("broker.hivemq.com")
  24. .Build();
  25. await mqttClient.ConnectAsync(mqttClientOptions);
  26. using (var mqttRpcClient = mqttFactory.CreateMqttRpcClient(mqttClient))
  27. {
  28. // Access to a fully featured application message is not supported for RCP calls!
  29. // The method will throw an exception when the response was not received in time.
  30. await mqttRpcClient.ExecuteAsync(TimeSpan.FromSeconds(2), "ping", "", MqttQualityOfServiceLevel.AtMostOnce);
  31. }
  32. Console.WriteLine("The RPC call was successful.");
  33. }
  34. }
  35. /*
  36. * The device must respond to the request using the correct topic. The following C code shows how a
  37. * smart device like an ESP8266 must respond to the above sample.
  38. *
  39. // If using the MQTT client PubSubClient it must be ensured
  40. // that the request topic for each method is subscribed like the following.
  41. mqttClient.subscribe("MQTTnet.RPC/+/ping");
  42. mqttClient.subscribe("MQTTnet.RPC/+/do_something");
  43. // It is not allowed to change the structure of the topic.
  44. // Otherwise RPC will not work.
  45. // So method names can be separated using an _ or . but no +, # or /.
  46. // If it is required to distinguish between devices
  47. // own rules can be defined like the following:
  48. mqttClient.subscribe("MQTTnet.RPC/+/deviceA.ping");
  49. mqttClient.subscribe("MQTTnet.RPC/+/deviceB.ping");
  50. mqttClient.subscribe("MQTTnet.RPC/+/deviceC.getTemperature");
  51. // Within the callback of the MQTT client the topic must be checked
  52. // if it belongs to MQTTnet RPC. The following code shows one
  53. // possible way of doing this.
  54. void mqtt_Callback(char *topic, byte *payload, unsigned int payloadLength)
  55. {
  56. String topicString = String(topic);
  57. if (topicString.startsWith("MQTTnet.RPC/")) {
  58. String responseTopic = topicString + String("/response");
  59. if (topicString.endsWith("/deviceA.ping")) {
  60. mqtt_publish(responseTopic, "pong", false);
  61. return;
  62. }
  63. }
  64. }
  65. // Important notes:
  66. // ! Do not send response message with the _retain_ flag set to true.
  67. // ! All required data for a RPC call and the result must be placed into the payload.
  68. */
  69. }