public static IMqttRpcClient CreateMqttRpcClient(this MqttFactory factory, IMqttClient mqttClient)
 {
     return(factory.CreateMqttRpcClient(mqttClient, new MqttRpcClientOptions
     {
         TopicGenerationStrategy = new DefaultMqttRpcClientTopicGenerationStrategy()
     }));
 }
Beispiel #2
0
    /*
     * The extension MQTTnet.Extensions.Rpc (available as nuget) allows sending a request and waiting for the matching reply.
     * This is done via defining a pattern which uses the topic to correlate the request and the response.
     * From client usage it is possible to define a timeout.
     */

    public static async Task Send_Request()
    {
        var mqttFactory = new MqttFactory();

        // The RPC client is an addon for the existing client. So we need a regular client
        // which is wrapped later.

        using (var mqttClient = mqttFactory.CreateMqttClient())
        {
            var mqttClientOptions = new MqttClientOptionsBuilder()
                                    .WithTcpServer("broker.hivemq.com")
                                    .Build();

            await mqttClient.ConnectAsync(mqttClientOptions);

            using (var mqttRpcClient = mqttFactory.CreateMqttRpcClient(mqttClient))
            {
                // Access to a fully featured application message is not supported for RCP calls!
                // The method will throw an exception when the response was not received in time.
                await mqttRpcClient.ExecuteAsync(TimeSpan.FromSeconds(2), "ping", "", MqttQualityOfServiceLevel.AtMostOnce);
            }

            Console.WriteLine("The RPC call was successful.");
        }
    }