跳到主要内容

用法

配置网络和客户端配对

首先确保正确配置了网络和客户端配对

使用Client实例

Chat Client是一个单例,您可以通过调用来访问它

Chat.instance

在密钥服务器上注册地址

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

let Caip2Account = Account(chainIdentifier: "eip155:1", address: "0x36275231673672234423f")!
let pubKey = try! await Chat.instance.register(account: Caip2Account)

解析与它的地址相关的对等公钥

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

let resolvedPubKey = try! await Chat.instance.resolve(account: Caip2Account)

邀请其他人加入聊天

try! await Chat.instance.invite(publicKey: resolvedPubKey, peerAccount: peerAccount, openingMessage: "hello", account: myAccount)

通过邀请

当收到邀请时,您可以接受邀请或拒绝邀请。

try! await Chat.instance.accept(inviteId: invite.id)
try! await Chat.instance.reject(inviteId: invite.id)

发送消息

try! await Chat.instance.message(topic: thread.topic, message: "Hello!")

离开聊天

你可以通过调用:

try! await Chat.instance.leave(topic: thread.topic)

客户端事件

集成聊天客户端将需要您处理以下事件:

newThreadPublisher 连接建立事件

public var newThreadPublisher: AnyPublisher<Thread, Never> 

invitePublisher 收到邀请事件

public var invitePublisher: AnyPublisher<Invite, Never> 

messagePublisher 收到消息事件

public var messagePublisher: AnyPublisher<Message, Never>