From 6608172719fc8189686e39675d22511d8d473989 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Sun, 7 Jul 2019 21:26:07 +0200 Subject: [PATCH] MQTTnet.Server: Extend messages API. --- .../Controllers/MessagesController.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Source/MQTTnet.Server/Controllers/MessagesController.cs b/Source/MQTTnet.Server/Controllers/MessagesController.cs index 89eb385..6bd00e7 100644 --- a/Source/MQTTnet.Server/Controllers/MessagesController.cs +++ b/Source/MQTTnet.Server/Controllers/MessagesController.cs @@ -1,7 +1,9 @@ using System; +using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using MQTTnet.Protocol; using MQTTnet.Server.Mqtt; namespace MQTTnet.Server.Controllers @@ -27,14 +29,23 @@ namespace MQTTnet.Server.Controllers [Route("api/v1/messages/{*topic}")] [HttpPost] - public Task PostMessage(string topic, string payload) + public async Task PostMessage(string topic, int qosLevel = 0) { + byte[] payload; + + using (var memoryStream = new MemoryStream()) + { + await HttpContext.Request.Body.CopyToAsync(memoryStream); + payload = memoryStream.ToArray(); + } + var message = new MqttApplicationMessageBuilder() .WithTopic(topic) .WithPayload(payload) + .WithQualityOfServiceLevel((MqttQualityOfServiceLevel)qosLevel) .Build(); - return PostMessage(message); + return await PostMessage(message); } } }