private suspend fun init(context: Context): Boolean = withContext(Dispatchers.Main) {
suspendCancellableCoroutine { continuation ->
Timber.i("初始化 野火 ${isInitialized}")
if (!isInitialized) {
ChatManager.init(context, "192.168.224.165")
ChatManager.Instance().addIMServiceStatusListener(object : IMServiceStatusListener {
override fun onServiceConnected() {
Timber.e("野火 im服务链接成功")
isInitialized = true
continuation.resume(isInitialized)
}
override fun onServiceDisconnected() {
Timber.e("野火 Im服务断开了")
continuation.resume(false)
}
})
ChatManager.Instance().apply {
startLog()
//发送日志命令,当发送此文本消息时,会把协议栈日志发送到当前会话中,为空时关闭此功能。
setSendLogCommand("*#marslog#")
addRecallMessageListener {
//撤回消息监听
Timber.i("撤回消息监听:${it.messageId}")
}
addOnMessageUpdateListener {
//消息更新监听
Timber.i("消息更新监听:${it.messageId}")
}
addConnectionChangeListener { status ->
//链接状态监听
Timber.i("链接状态监听:${status}")
}
}
} else {
continuation.resume(true)
}
}
}
val initResult = init(context)
Timber.i("野火是否初始化 ${initResult} 线程 ${Thread.currentThread().name} ")
if (!initResult) {
return false
}
Timber.i("初始化野火成功 ")
if (isLogin(userId)) {
return true
}
this.userId = userId
val clientId= ChatManager.Instance().clientId
Timber.i("野火 clientId = ${clientId}")
val imToken = imTokenProvider.getWildfireIMTokenOrNull(clientId)
?: return false
//这是阻塞的,并且没有回调,所有链接结果需要在主动获取一边
Timber.i("野火 开始链接 userid= ${userId} token = ${imToken}")
val time = ChatManager.Instance().connect(userId, imToken)
Timber.i("野火 链接使用的时间:${time}")