40 行
1.4 KiB
Kotlin
40 行
1.4 KiB
Kotlin
package com.xuqm.sdk.im
|
|
|
|
import com.xuqm.sdk.XuqmSDK
|
|
import com.xuqm.sdk.im.api.ImApi
|
|
import com.xuqm.sdk.im.listener.ImEventListener
|
|
import com.xuqm.sdk.im.model.ChatType
|
|
import com.xuqm.sdk.im.model.MsgType
|
|
import com.xuqm.sdk.network.ApiClient
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.launch
|
|
|
|
object ImSDK {
|
|
|
|
private var client: ImClient? = null
|
|
private val api: ImApi by lazy { ApiClient.create() }
|
|
private val scope = CoroutineScope(Dispatchers.IO)
|
|
|
|
fun login(appId: String, userId: String, nickname: String? = null, avatar: String? = null) {
|
|
XuqmSDK.requireInit()
|
|
scope.launch {
|
|
val res = api.login(appId, userId, nickname, avatar)
|
|
res.data?.token?.let { token ->
|
|
XuqmSDK.tokenStore.saveToken(token)
|
|
val wsUrl = XuqmSDK.config.imBaseUrl
|
|
client = ImClient(wsUrl, token, appId)
|
|
client?.connect()
|
|
}
|
|
}
|
|
}
|
|
|
|
fun sendMessage(toId: String, chatType: ChatType, msgType: MsgType, content: String) {
|
|
client?.sendMessage(toId, chatType, msgType, content)
|
|
}
|
|
|
|
fun addListener(listener: ImEventListener) = client?.addListener(listener)
|
|
fun removeListener(listener: ImEventListener) = client?.removeListener(listener)
|
|
fun disconnect() = client?.disconnect()
|
|
}
|