跳到主要内容

Chat 示例 App

我们建议在我们的Kotlin Github库上查看CHat Sample的示例实现。

初始化聊天客户端

val projectId = "" // Get Project ID at https://cloud.walletconnect.com/
val relayUrl = "relay.walletconnect.com"
val serverUrl = "wss://$relayUrl?projectId=$projectId"
val connectionType = ConnectionType.AUTOMATIC or ConnectionType.MANUAL
val appMetaData = Core.Model.AppMetaData(
name = "Chat Sample",
description = "Chat Sample description",
url = "Chat Sample Url",
icons = /*list of icon url strings*/,
redirect = "kotlin-chat-wc:/request" // Custom Redirect URI
)

CoreClient.initialize(relayServerUrl = serverUrl, connectionType = connectionType, application = this, metaData = appMetaData)

val init = Chat.Params.Init(coreClient = CoreClient)

ChatClient.initalize(init) { error ->
// Error will be thrown if there's an isssue during initalization
}

初始化聊天客户端, 创建一个 Chat.Params.Init 对象在Android Application class 在Core Client的 onCreate方法. 然后将Chat.Params.Init对象传递给ChatClient初始化函数。

ChatClient.ChatDelegate

val chatDelegate = object : ChatClient.ChatDelegate {
override fun onInvite(onInvite: Chat.Model.Events.OnInvite) {
// Triggered when a new invite is received
}

override fun onJoined(onJoined: Chat.Model.Events.OnJoined) {
// Triggered when a new chat thread joined
}

override fun onReject(onReject: Chat.Model.Events.OnReject) {
// Triggered when a intive is rejected by the other peer
}

override fun onMessage(onMessage: Chat.Model.Events.OnMessage) {
// Triggered when a new chat message is received
}

override fun onLeft(onLeft: Chat.Model.Events.OnLeft) {
// Triggered when a chat thread is left by a peer
}

override fun onConnectionStateChange(state: Sign.Model.ConnectionState) {
//Triggered whenever the connection state is changed
}

override fun onError(error: Sign.Model.Error) {
// Triggered whenever there is an issue inside the SDK
}
}
ChatClient.setChatDelegate(chatDelegate)

ChatClient需要传递给它一个 ChatClient.ChatDelegate,以便能够公开从另一个对等端发送的异步更新。

方法

在密钥服务器上注册地址

要使用户的帐户公开发现,必须在公钥服务器上注册他的帐户。Keyserver将创建一个帐户和客户端生成的公钥记录,用于聊天创建期间的双方密钥交换。

val register = Chat.Params.Register(Chat.Model.AccountId(account = /*[CAIP-10](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md) compatible accountId*/))
ChatClient.register(register, object : Chat.Listeners.Register {
override fun onError(error: Chat.Model.Error) {
//Error while registering an address
}

override fun onSuccess(publicKey: String) {
//Address registered successfully
}
})

用peer的地址注册peer的公钥

若要解析需要邀请对方进入聊天线程的公钥,请调用resolve方法。

val resolve = Chat.Params.Resolve(Chat.Model.AccountId(account = /*[CAIP-10](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md) compatible accountId*/)
ChatClient.resolve(resolve), object : Chat.Listeners.Resolve {
override fun onError(error: Chat.Model.Error) {
//Error occured
}

override fun onSuccess(publicKey: String) {
//Public key found for given account address
}
})

邀请聊天

val seflAccountId = Chat.Model.AccountId(account = registered /*[CAIP-10](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md) compatible accountId*/)
val inviteModel = Chat.Model.Invite(seflAccountId, openingMessage, resolvedPublicKey)
val invite = Chat.Params.Invite(Chat.Model.AccountId(peerAccountId), inviteModel)

ChatClient.invite(invite) { error -> Log.e(tag(this), error.throwable.stackTraceToString()) }

处理邀请请求

val acceptParams = Chat.Params.Accept(inviteId)
ChatClient.accept(acceptParams,
onSuccess = { threadTopic -> /* Thread topic */ },
onError = { error -> /* Error when accepting */ })
val rejectParams = Chat.Params.Reject(inviteId)
ChatClient.accept(rejectParams, onError = { error -> /* Error when rejecting */ })

发送消息

val threadTopic = /*thread topic*/
val author = Chat.Model.AccountId(/*[CAIP-10](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md) compatible accountId*/)
val messageParams = Chat.Params.Message(threadTopic, author, message)

ChatClient.message(messageParams) { error -> /* Error while sending a message */ }

离开聊天

val threadTopic = /*thread topic*/

ChatClient.leave(threadTopuic) { error -> /* Error while leaving a thread */ }