

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許可。