Comments (3)
You must change a few things in order to get it running:
- You must use protocol version 5 which can be set in the client options builder
- Do not set ProcessPublish to false because then the message will basically treated as never sent
- You must use QoS 1 so that the client will receive a response. QoS 0 will not receive and response from the server
- When using protocol version 5 you must have a subscriber. Otherwise you will get a "NotMatchingSubscribers" response
from mqttnet.
I adjusted the unit test based on the guide above and it is now working
[Test]
public async Task TestCancelPublish()
{
var testTopic = "test/topic";
var mqttFactory = new MqttFactory();
var mqttServerOptions = new MqttServerOptionsBuilder()
.WithDefaultEndpoint()
.Build();
using (var mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions))
{
await mqttServer.StartAsync();
bool messageInterceptedOnServer = false;
mqttServer.InterceptingPublishAsync += async (args) =>
{
// intercept server message and set reason code to NotAuthorized
args.Response.ReasonCode = MqttPubAckReasonCode.NotAuthorized;
args.Response.ReasonString = "Not Authorized";
messageInterceptedOnServer = true;
};
using (var client1 = mqttFactory.CreateMqttClient())
{
var client1Options = new MqttClientOptionsBuilder()
.WithTcpServer("localhost")
.WithClientId(Guid.NewGuid().ToString())
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
.Build();
await client1.ConnectAsync(client1Options, CancellationToken.None);
using(var client2 = mqttFactory.CreateMqttClient())
{
var client2Options = new MqttClientOptionsBuilder()
.WithTcpServer("localhost")
.WithClientId(Guid.NewGuid().ToString())
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V500)
.Build();
await client2.ConnectAsync(client2Options, CancellationToken.None);
await client2.SubscribeAsync(testTopic,MqttQualityOfServiceLevel.AtLeastOnce);
// publish the message
var res = await client1.PublishStringAsync(testTopic, "hello",MqttQualityOfServiceLevel.AtLeastOnce);
while (messageInterceptedOnServer == false)
{
await Task.Delay(300);
}
await client2.DisconnectAsync();
// Now get not authorized
Assert.AreEqual(res.ReasonCode, MqttClientPublishReasonCode.NotAuthorized);
}
await client1.DisconnectAsync();
}
await mqttServer.StopAsync();
}
}
It might not be Mqtt standard but only sending NoMatchingSubscribers if you are authorized in the MqttClientSessionsManager class would be a cleaner solution for me. If you have retained messages on I think it is ok that you do not have any subscribers.
eg
if (matchingSubscribersCount == 0 && reasonCode != (int)MqttPubAckReasonCode.NotAuthorized)
{
reasonCode = (int)MqttPubAckReasonCode.NoMatchingSubscribers;
await FireApplicationMessageNotConsumedEvent(applicationMessage, senderId).ConfigureAwait(false);
}
from mqttnet.
That is a good idea. But would check for success instead. So that it will never return NoMatchingSubscribers only if it is basically a success and no error etc.
from mqttnet.
Related Issues (20)
- MqttClient Publish a message when you are inside the `InterceptingPublishAsync` result to an Error when the Client Publish a QoS 1
- MqttServer messages send get lost while client is subscribing HOT 2
- Unable to use certificate in Ubuntu (TLS1.2) HOT 1
- MqttClient with TLS and client certificate works on Linux but not on windows using the latest and following samples HOT 18
- No valid replacement for the obsolete MqttClientOptionsBuilder.WithConnectionUri HOT 1
- MQTTnet.AspTestApp Fails to start HOT 1
- MQTTnet.Extensions.WebSocket4Net uses several outdated and possibly vulnerable dependencies HOT 4
- MQTT-over-Websocket connection reconnects every 30 seconds (MAUI regression) HOT 1
- Can't connect to broker using CA Cert ClientCert and Key HOT 2
- Unable to connect with Unix Socket HOT 4
- Last Will not working? HOT 2
- [FR] Protobuf HOT 4
- PublishAsync doesn't return a result nor exception HOT 4
- Linux - when Upgrading from version 4.3.3.952 to 4.3.4.1084 the client always raises the exception «None of the discovered or specified addresses match the socket address family» HOT 7
- Restore messages per client/subscription HOT 2
- No connection could be made because the target machine actively refused it. HOT 1
- Breaking Change in MqttClientTcpOptions with 4.3.5.1141 HOT 5
- Managed MQTT client hangs when trying to disconnect using `StopAsync()` in Unity Engine HOT 4
- UWP PublishAsync forever hangs
- Event when pending ApplicationMessage was published HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mqttnet.