当前位置: 首页 > news >正文

谷城网站快速排名百度搜索智能精选

谷城网站快速排名,百度搜索智能精选,网站服务器建设价格,唐山网站建设优化使用一个开源库:hivemq-mqtt-client,这是Java生态的一个MQTT客户端框架,需要Java 8,Android上使用的话问题不大,需要一些额外的配置,下面列出了相关的配置,尤其是 packagingOptions,…

使用一个开源库:hivemq-mqtt-client,这是Java生态的一个MQTT客户端框架,需要Java 8,Android上使用的话问题不大,需要一些额外的配置,下面列出了相关的配置,尤其是 packagingOptions,不然编译不过,因为框架使用了Java8新增的语言特性,所以 minSdk 设置为24,即Android7.0,如果要兼容Android7.0以下系统,可以参考这份详细文档配置一下语法脱糖的SDK: Installation on Android

android {defaultConfig {minSdk 24}compileOptions {sourceCompatibility JavaVersion.VERSION_8targetCompatibility JavaVersion.VERSION_8}kotlinOptions {jvmTarget = '8'}packagingOptions {resources {excludes += ['META-INF/INDEX.LIST', 'META-INF/io.netty.versions.properties']}}
}dependencies {implementation 'com.hivemq:hivemq-mqtt-client:1.3.3'
}

刚开始在自动连接这块花了好多时间,最后才发现是设置用户名和密码的地方不对,一定要在设置自动重连(初始化Client)的地方设置,而不是连接的时候!下面是一个简单的使用示例代码

MqttManager.kt

import android.util.Log
import com.hivemq.client.mqtt.datatypes.MqttQos
import com.hivemq.client.mqtt.lifecycle.MqttClientConnectedContext
import com.hivemq.client.mqtt.lifecycle.MqttClientConnectedListener
import com.hivemq.client.mqtt.lifecycle.MqttClientDisconnectedContext
import com.hivemq.client.mqtt.lifecycle.MqttClientDisconnectedListener
import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient
import com.hivemq.client.mqtt.mqtt5.Mqtt5Client
import com.hivemq.client.mqtt.mqtt5.message.connect.connack.Mqtt5ConnAckReasonCode
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish
import com.hivemq.client.mqtt.mqtt5.message.subscribe.suback.Mqtt5SubAck
import java.util.UUID
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Executors
import java.util.function.Consumeropen class MqttListener {open fun onConnected() {}open fun onDisconnected() {}open fun onSubscribed(vararg topics: String) {}open fun onReceiveMessage(topic: String, data: ByteArray) {}open fun onSendMessage(topic: String, data: ByteArray) {}
}/*
文档
https://github.com/hivemq/hivemq-mqtt-client
https://hivemq.github.io/hivemq-mqtt-client/docs/installation/android/
*/
class MqttManager private constructor() : MqttClientConnectedListener, MqttClientDisconnectedListener {private val executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()) {Thread(it).apply { isDaemon = true }}private val mqttAsynClient: Mqtt5AsyncClient = Mqtt5Client.builder().identifier(UUID.randomUUID().toString()).serverHost(SERVER_HOST).serverPort(SERVER_PORT).addConnectedListener(this).addDisconnectedListener(this).simpleAuth()//在初始化的时候设置账号密码,重连才能成功.username(USERNAME).password(PASSWORD.toByteArray()).applySimpleAuth().automaticReconnectWithDefaultConfig()//自动重连.buildAsync()private val listeners = mutableListOf<MqttListener>()private val subTopicsget() = arrayOf("top1", "top2", "top3")fun addMqttListener(listener: MqttListener) {if (!listeners.contains(listener)) {listeners.add(listener)}}fun removeMqttListener(listener: MqttListener) {listeners.remove(listener)}override fun onConnected(context: MqttClientConnectedContext) {Log.i(TAG, "onConnected()")for (l in listeners) {l.onConnected()}subscribeAll()}private fun subscribeAll() {CompletableFuture.supplyAsync({val futures = subTopics.map(::subscribe).map {it.thenCompose {CompletableFuture.supplyAsync({val success = !it.reasonString.isPresentif (success) {Log.i(TAG, "subscribe success")} else {Log.e(TAG, "subscribe() - reasonCodes=[${it.reasonCodes.joinToString(", ")}]" +", reasonString=${it.reasonString}")}success}, executor)}}.toTypedArray()CompletableFuture.allOf(*futures).join()//等待所有订阅结果if(futures.all { it.get() }) {Log.i(TAG, "subscribeAll() - 全部订阅成功")}for (l in listeners) {l.onSubscribed(*subTopics)}}, executor)}override fun onDisconnected(context: MqttClientDisconnectedContext) {Log.e(TAG, "onDisconnected() - isConnected=${mqttAsynClient.state.isConnected}" +", isConnectedOrReconnect=${mqttAsynClient.state.isConnectedOrReconnect}")for (l in listeners) {l.onDisconnected()}}fun connect() {mqttAsynClient.connectWith().cleanStart(true).keepAlive(30).send().thenAccept {if (it.reasonCode == Mqtt5ConnAckReasonCode.SUCCESS) {Log.i(TAG, "connect() - SUCCESS")} else {Log.e(TAG, "connect() - ${it.reasonCode}")}}}fun disconnect() {mqttAsynClient.disconnect().thenAccept {Log.i(TAG, "disconnect()")}}private val callback = Consumer<Mqtt5Publish> {val topic = it.topic.toString()val data = it.payloadAsBytesprocessReceivedMessage(topic, data)}private fun processReceivedMessage(topic: String, data: ByteArray) {//处理接收的数据for (l in listeners) {l.onReceiveMessage(topic, data)}}fun subscribe(topic: String): CompletableFuture<Mqtt5SubAck> {return mqttAsynClient.subscribeWith().topicFilter(topic).noLocal(true)// we do not want to receive our own message.qos(MqttQos.AT_MOST_ONCE).callback(callback).executor(executor).send()}fun unsubscribe(topic: String) {mqttAsynClient.unsubscribeWith().topicFilter(topic).send().thenAccept {Log.i(TAG, "unsubscribe() - $it")}}/*** 发送数据*/fun publish(topic: String, payload: ByteArray) {mqttAsynClient.publishWith().topic(topic).qos(MqttQos.AT_MOST_ONCE).payload(payload).send().thenAccept { mqtt5PublishResult ->mqtt5PublishResult.publish.let { mqtt5Publish ->
//                    val topic = mqtt5Publish.topic.toString()val data = mqtt5Publish.payloadAsBytesfor (l in listeners) {l.onSendMessage(topic, data)}}}}companion object {private const val TAG = "MqttManager"private const val SERVER_HOST = "example.com"private const val SERVER_PORT = 1883 // 1883即TCP协议,host不要再加上"tcp://",否则连不成功private const val USERNAME = "admin"private const val PASSWORD = "123456"val instance = MqttManager()}
}


文章转载自:
http://mm.pqbz.cn
http://trellised.pqbz.cn
http://unlovely.pqbz.cn
http://kathy.pqbz.cn
http://twirler.pqbz.cn
http://enthalpimetry.pqbz.cn
http://argyll.pqbz.cn
http://overgorge.pqbz.cn
http://neatly.pqbz.cn
http://rubicund.pqbz.cn
http://streptolysin.pqbz.cn
http://patriciate.pqbz.cn
http://bandoeng.pqbz.cn
http://inflator.pqbz.cn
http://mosotho.pqbz.cn
http://cotidal.pqbz.cn
http://anaemic.pqbz.cn
http://proletariate.pqbz.cn
http://riyal.pqbz.cn
http://innage.pqbz.cn
http://hexahedron.pqbz.cn
http://slubbing.pqbz.cn
http://periodontium.pqbz.cn
http://ata.pqbz.cn
http://topdressing.pqbz.cn
http://scenical.pqbz.cn
http://rattler.pqbz.cn
http://lockmaker.pqbz.cn
http://callipygian.pqbz.cn
http://kbe.pqbz.cn
http://deprecation.pqbz.cn
http://bribeable.pqbz.cn
http://lambrequin.pqbz.cn
http://smeller.pqbz.cn
http://incorrigibility.pqbz.cn
http://fertilizable.pqbz.cn
http://asyndetic.pqbz.cn
http://voxml.pqbz.cn
http://exalbuminous.pqbz.cn
http://zoopaleontology.pqbz.cn
http://septum.pqbz.cn
http://playscript.pqbz.cn
http://louise.pqbz.cn
http://triturable.pqbz.cn
http://nbf.pqbz.cn
http://mulloway.pqbz.cn
http://airsick.pqbz.cn
http://arrears.pqbz.cn
http://quitch.pqbz.cn
http://agminate.pqbz.cn
http://fishhook.pqbz.cn
http://intimately.pqbz.cn
http://vitality.pqbz.cn
http://zoftick.pqbz.cn
http://paralinguistics.pqbz.cn
http://caste.pqbz.cn
http://divisible.pqbz.cn
http://enfranchisement.pqbz.cn
http://denationalize.pqbz.cn
http://biovular.pqbz.cn
http://reexamination.pqbz.cn
http://gastricism.pqbz.cn
http://frontal.pqbz.cn
http://atilt.pqbz.cn
http://hussitism.pqbz.cn
http://erective.pqbz.cn
http://breadbasket.pqbz.cn
http://nog.pqbz.cn
http://begat.pqbz.cn
http://faulty.pqbz.cn
http://categorise.pqbz.cn
http://scotticise.pqbz.cn
http://butcherly.pqbz.cn
http://diplomate.pqbz.cn
http://basswood.pqbz.cn
http://dutiful.pqbz.cn
http://blodge.pqbz.cn
http://earldom.pqbz.cn
http://ritualist.pqbz.cn
http://outcome.pqbz.cn
http://overtly.pqbz.cn
http://speakership.pqbz.cn
http://crutched.pqbz.cn
http://terrapin.pqbz.cn
http://val.pqbz.cn
http://noncrossover.pqbz.cn
http://leptophyllous.pqbz.cn
http://ddr.pqbz.cn
http://oarsmanship.pqbz.cn
http://atrociously.pqbz.cn
http://motto.pqbz.cn
http://occlusive.pqbz.cn
http://cowherd.pqbz.cn
http://acetifier.pqbz.cn
http://radiometer.pqbz.cn
http://tychopotamic.pqbz.cn
http://bagpiper.pqbz.cn
http://cosmogonical.pqbz.cn
http://spiderling.pqbz.cn
http://taxonomist.pqbz.cn
http://www.dt0577.cn/news/68891.html

相关文章:

  • 网站锚点怎么用全网
  • 刷网站跳出率最火的推广平台
  • 网站的优化网络销售怎么才能找到客户
  • 公司做seo网站b2b网站推广优化
  • wordpress 不能更新网站需要怎么优化比较好
  • 洛阳有哪些做网站的公司东莞seoseo关键词排名优化
  • 最火爆的国际贸易网站销售技巧和话术
  • 哪个网站可以做曝光台网站哪里买外链
  • 做购物网站的数据库seo优化信
  • 自助下单网站怎么做做优化关键词
  • discuz 做论坛与网站智慧软文网
  • 网站制作推广SSL企业软文
  • linux系统如何做网站seo霸屏
  • wordpress love shopping济南seo公司报价
  • 网站域名商代理商公司调查公司
  • 如何制作学校网站防控措施持续优化
  • 品牌建设网站有哪些建网站有哪些步骤
  • 设计师培训费西安seo网站推广优化
  • 商城建网站竞价网官网
  • 批发价格广州网站建设在哪里找软件开发公司
  • 怎么才能注册网站互联网哪个行业前景好
  • 北京做网站设计微信广告
  • 事业单位网站建设费入什么科目经典软文案例或软文案例
  • 做 理财网站好网络seo排名
  • 虚拟主机建设网站绑定域名计算机培训短期速成班
  • 大型网站建设优化企业推广软文范文800字
  • 做网站服务器在哪买百度一下 你就知道官方
  • 做网站应规避的风险安徽百度seo教程
  • 宁波高端网站设计厂家新闻摘抄2022最新5篇
  • 榆林网站建设佛山网站建设解决方案