在Flutter中实现MQTT [英] Implementing MQTT in Flutter

查看:280
本文介绍了在Flutter中实现MQTT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flutter的新手(来自Android背景),想在Flutter中实现MQTT客户端.

I am new to Flutter (coming from Android background) and want to implement MQTT client in flutter.

这就是我想要的:

  1. MQTT客户端,当应用程序打开/未被操作系统杀死时,该客户端应处于活动状态".

  1. MQTT client which should be "alive" when the app is open/not killed by OS.

我不想在后台运行它(在未打开应用程序的情况下,我知道在iOS中执行该问题,请在某处阅读),但是在应用程序处于打开状态时.

I don't want to run it in background (without the app open, i know there is issue doing it in iOS, read it somewhere) but while the app is open.

根据订阅主题中的消息更新UI/状态.

Updating UI/State based on messages coming in subscribed topics.

我查看了pub,发现了2个软件包, mqtt

I looked into pub and found 2 packages, mqtt and mqtt_client, they look like non-Flutter implementation of MQTT in dart.

我有一个想法,如何使用此库中的一个使它正常工作,但不确定是否可以工作:

I have an idea how to use one of this lib to make it work with flutter but not sure if it will work:

  1. 创建并实现MQTTObserver满意的小部件,该小部件将异步连接到代理并侦听特定主题的消息并相应地更新状态.

  1. Create and implement MQTTObserver sateful widget which will async-ly connect to broker and listen for messages to particular topic and update the state accordingly.

将此小部件包装在MaterialApp周围

Will wrap this widget around MaterialApp

树下的任何小部件都将能够访问MQTTObserver小部件的状态以在其UI中反映出来.

Any widget down the tree would be able to access the state of MQTTObserver widget to reflect it in their UI.

您认为这是个好主意吗?能行吗?你有更好的主意吗?还是应该为MQTT实施平台特定的实现(应该是我的最后选择).

Do you think it is a good idea? will it work? do you have better idea? or should i implement platform-specific implementation for MQTT (should be my last option).

推荐答案

尽管正在开发中,但mfltt_client软件包已在flutter中使用,我自己并未使用flutter,但一些flutter用户对此提出了疑问,请参阅[此处] [1]

The mqtt_client package is already being used in flutter although under development, I don't use flutter myself but several flutter users have raised issues on it, see [here][1]

[1]: https://github.com/shamblett/mqtt_client/issues/16例如,问题列表中还有其他内容,也许您可​​以问一个扑扑的开发者中的一个.

[1]: https://github.com/shamblett/mqtt_client/issues/16 for an example, there others in the issues list, maybe you could ask one of these flutter devs your question.

这篇关于在Flutter中实现MQTT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆