

Courier是一个Kotlin库,用于使用MQTT协议创建长期运行的连接。
长期运行的连接是用于即时双向通信的客户端和服务器之间建立的持久连接。借助Keep Alive数据包,可在最大可能的持续时间内保持长期运行的连接。这有助于节省移动设备上的电池和数据。
MQTT是一种非常轻巧的协议,可用于发布/订阅消息模型。它设计用于与需要“小型代码足迹”或网络带宽有限的远程位置的连接。
该协议通常通过TCP/IP运行;但是,任何提供有序,无损,双向连接的网络协议都可以支持MQTT。
MQTT有3个内置QoS级别用于可靠消息传递:
QoS 0(最多一次) - 该消息仅发送一次,客户和经纪人没有采取其他步骤来确认交货(开火和忘记)。
QoS 1(至少一次) - 发送者多次重新调整该消息,直到收到确认为止(已确认的交付)。
QoS 2(恰好一次) - 发件人和接收器进行两级握手,以确保收到消息的一份副本(保证的交付)。
在此处找到详细的文档-https://gojek.github.io/courier-android/
端到端的快递示例-https://gojek.github.io/courier/docs/introduction
清洁API
自适应保持活力
消息和流适配器
订阅商店
自动重新连接和重新订阅
数据库持久性
背压处理
警报,计时器和工人ping发件人
Mqtt Chuck
有关快递库中功能的更多详细信息可以在此处找到
在此处添加了一个演示应用程序,该应用程序与HiveMQ公共经纪人建立了快递连接。
所有快递图书馆的文物都可以通过Maven Central获得。
repositories {
mavenCentral()
}
dependencies {
implementation " com.gojek.courier:courier:x.y.z "
implementation " com.gojek.courier:courier-message-adapter-gson:x.y.z "
implementation " com.gojek.courier:courier-stream-adapter-rxjava2:x.y.z "
}声明诸如发送,接收,订阅,取消订阅的操作的服务接口:
interface MessageService {
@Receive(topic = " topic/{id}/receive " )
fun receive (@Path( " id " ) identifier : String ): Observable < Message >
@Send(topic = " topic/{id}/send " , qos = QoS . TWO )
fun send (@Path( " id " ) identifier : String , @Data message : Message )
@Subscribe(topic = " topic/{id}/receive " , qos = QoS . ONE )
fun subscribe (@Path( " id " ) identifier : String ): Observable < Message >
@Unsubscribe(topics = [ " topic/{id}/receive " ])
fun unsubscribe (@Path( " id " ) identifier : String )
}使用快递来创建实施:
val mqttClient = MqttClientFactory .create(
context = context,
mqttConfiguration = MqttV3Configuration (
authenticator = authenticator
)
)
val courierConfiguration = Courier . Configuration (
client = mqttClient,
streamAdapterFactories = listOf ( RxJava2StreamAdapterFactory ()),
messageAdapterFactories = listOf ( GsonMessageAdapter . Factory ())
)
val courier = Courier (courierConfiguration)
val messageService = courier.create< MessageService >()messageService.subscribe( " user-id " ).subscribe { message ->
print (message)
}
messageService.unsubscribe( " user-id " )messageService.send( " user-id " , message)
messageService.receive( " user-id " ) { message ->
print (message)
} val connectOptions = MqttConnectOptions (
serverUris = listOf ( ServerUri ( SERVER_URI , SERVER_PORT )),
clientId = clientId,
username = username,
keepAlive = KeepAlive (
timeSeconds = keepAliveSeconds
),
isCleanSession = cleanSessionFlag,
password = password
)
mqttClient.connect(connectOptions)mqttClient.disconnect()此选项允许您在MQTT v3.1.1的Connect数据包中发送用户范围。
val connectOptions = MqttConnectOptions (
serverUris = listOf ( ServerUri ( SERVER_URI , SERVER_PORT )),
clientId = clientId,
.. .
userPropertiesMap = mapOf (
" key1 " to " value1 " ,
" key2 " to " value2 "
)
)
mqttClient.connect(connectOptions)阅读我们的贡献指南,以了解我们的开发过程,如何提出错误修正和改进以及如何构建和测试您对快递Android库的更改。
除PAHO以外的所有快递模块都获得了MIT许可。 Paho已获得Eclipse许可。