Comments (5)
I created a Unit Test with your code and it runs fine.
Does this also occur with later versions?
from mqttnet.
My recommendation is to avoid the managed client in this case. The regular client gives you more flexibility. But it requires manual reconnecting etc. Please let me know if the regular client has the same issues in Unity.
from mqttnet.
I am using version 4.3.5 which I believe is the latest version which I installed from NuGet.
Thank you for running that test. I also ran the code above as a unit test and it does indeed work fine. I think the problem is happening when I run this same code inside Unity Editor for the Unity Game Engine. (I should've explained this context before, but I assumed the problem was with the MQTTnet library itself.)
It seems like there's something in StopAsync()
or MaintainConnectionAsync()
that works differently in the context of a Unity Engine environment.
Is there a way to set a disconnect timeout so that StopAsync()
will throw an exception after x seconds?
The code examples for the two unit tests are below:
This works:
// NOT in Unity Engine
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Extensions.ManagedClient;
namespace TestProject1;
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
// https://github.com/dotnet/MQTTnet/wiki/Client#client-options
var options = new MqttClientOptionsBuilder()
.WithTcpServer("broker.emqx.io", 1883)
.Build();
var managedOptions = new ManagedMqttClientOptionsBuilder()
.WithClientOptions(options)
.Build();
var mqttFactory = new MqttFactory();
var client = mqttFactory.CreateManagedMqttClient();
var task = client.StartAsync(managedOptions);
task.Wait();
task = client.StopAsync(cleanDisconnect: false);
task.Wait();
Assert.Pass();
}
}
This Unity code hangs:
// Unity Engine testing with NUnit
using NUnit.Framework;
using UnityEngine;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Extensions.ManagedClient;
namespace MyProject.Core.Tests
{
public class MqttNetTests
{
/// <summary>
/// Test the mqttnet library on its own.
/// </summary>
[Test]
public void MqttNetStopAsync()
{
// Set up MQTT client
var options = new MqttClientOptionsBuilder()
.WithTcpServer("broker.emqx.io", 1883)
.Build();
var managedOptions = new ManagedMqttClientOptionsBuilder()
.WithClientOptions(options)
.Build();
var mqttFactory = new MqttFactory();
var client = mqttFactory.CreateManagedMqttClient();
// Connect
var task = client.StartAsync(managedOptions);
task.Wait();
Debug.Log("MQTT client started.");
// Disconnect
task = client.StopAsync(cleanDisconnect: false);
task.Wait(); // HANGS
Debug.Log("MQTT client stopped.");
Assert.Pass();
}
}
}
from mqttnet.
The unmanaged client works fine with Unity. Thanks for your help Christian @chkr1011
from mqttnet.
I am seeing a similar hang when calling StopAsync
from a SpecFlow test (no Unity involved, but I may be doing stuff to paint myself into a similar corner).
My problem can be alleviated if I add a .ConfigureAwait(false)
to the Task.WhenAny
call on line 286 here:
MQTTnet/Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs
Lines 284 to 288 in 25c1da6
from mqttnet.
Related Issues (20)
- nuget version of MQTTnet.Extensions.TopicTemplate is not up to date HOT 2
- Dose MQTTnet support Native AOT? HOT 1
- Reopen: nuget version of MQTTnet.Extensions.TopicTemplate is not up to date HOT 5
- When using InjectApplicationMessage the client does not receive any message when the topic starts with `$` HOT 5
- How to reconnect properly HOT 2
- sendmessage and mqtt service return function_clause
- Server subscriber sessions hash set can contain sessions with no subscriptions
- how to unsubscibe all topics HOT 1
- Enhancements for the MqttPacketIdentifierProvider HOT 2
- Can support save session to disk or database HOT 1
- Timing of ConnectAsync HOT 1
- Example of WebSocket broker from wiki does not work HOT 3
- MqttNet Connection not established after the Mqttnet Client is Disposed. in MQTTNet Version 4.3.3.952
- MqttClientFactory not fount in MQTTnet.4.3.6.1152 HOT 2
- How to start a MQTT Broker with WebSocket support in .NET Framework 4.6.1? HOT 1
- MQTTClient Thread-safety: Publishing HOT 4
- MQTTClient Thread-safety: Publishing
- Received packet 'PubAck: [PacketIdentifier=6538] [ReasonCode=Success]' at an unexpected time. HOT 3
- ReuseAddress option on Client Socket (as well).
- Certificate Authentication using PFX in .NET472 To Azure HOT 2
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.