mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-31 03:02:39 +08:00
MQTTnet.Server: Extend messages API.
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using MQTTnet.Protocol;
|
||||||
using MQTTnet.Server.Mqtt;
|
using MQTTnet.Server.Mqtt;
|
||||||
|
|
||||||
namespace MQTTnet.Server.Controllers
|
namespace MQTTnet.Server.Controllers
|
||||||
@@ -27,14 +29,23 @@ namespace MQTTnet.Server.Controllers
|
|||||||
|
|
||||||
[Route("api/v1/messages/{*topic}")]
|
[Route("api/v1/messages/{*topic}")]
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Task<ActionResult> PostMessage(string topic, string payload)
|
public async Task<ActionResult> 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()
|
var message = new MqttApplicationMessageBuilder()
|
||||||
.WithTopic(topic)
|
.WithTopic(topic)
|
||||||
.WithPayload(payload)
|
.WithPayload(payload)
|
||||||
|
.WithQualityOfServiceLevel((MqttQualityOfServiceLevel)qosLevel)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
return PostMessage(message);
|
return await PostMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user