gpt4 book ai didi

flutter - 如何在后台服务flutter中调用mqtt客户端

转载 作者:行者123 更新时间:2023-12-05 03:26:48 26 4
gpt4 key购买 nike

我的项目使用 mqtt 进行通知。并且需要实时通知。我有这样的 mqtt 类

class Mqtt {
...

Mqtt._() {
mqttClient = MqttServerClient.withPort(server, clientIdentifier, port);

///Successful connection callback
mqttClient.onConnected = _onConnected;

///Disconnected callback
mqttClient.onDisconnected = _onDisconnected;

///Subscription success callback
mqttClient.onSubscribed = _onSubscribed;

///Subscription failure callback
mqttClient.onSubscribeFail = _onSubscribeFail;
}

static Mqtt getInstance() {
if (_instance == null) {
_instance = Mqtt._();
}
return _instance!;
}

///connection
connect() {
mqttClient.connect();
_log("connecting");
}

...
}

我这样称呼

Mqtt mqtt = Mqtt.getInstance();
mqtt.subTopic = 'NOTIFICATION/';
mqtt.connect();

但是当应用程序停止时,mqtt 会断开连接。如何在后台服务flutter中调用mqtt客户端?

最佳答案

检查 flutter foreground service它可能会对您有所帮助。

关于flutter - 如何在后台服务flutter中调用mqtt客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71642718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com