我有这样的要求,我必须在左侧填充 1-1 个通道。通过频道列表,我必须在每个频道中显示频道中最后发送的消息。我怎样才能实现这个目标?
我想到了一个过程,我会
这是更好的方法还是还有其他方法可以做得更好?
你的方法听起来很棒,几个月前我把聊天演示放在一起时也遵循了类似的步骤:https://github.com/PubNubDevelopers/PubNub-Showcase/tree/main/web/chat。我将相关代码复制到这里:
生成加入频道列表
这将返回所有频道成员,您可以仅筛选您所属的频道。
pubnub.objects
.getChannelMembers({
channel: channel,
sort: { updated: 'desc' },
include: {
UUIDFields: true
},
totalCount: true
})
通过调用history API获取每个频道的最后一条消息
// Load channel history
pubnub
.fetchMessages({
channels: [channelName],
count: 20, // Limit to 20 messages. Design decision for this app, not a limitation of PubNUb
includeUUID: true,
includeMessageActions: true
})
然后将最后一条消息与频道的元数据相关联
我关联了上次阅读时间令牌,但您可以以相同的方式关联最后一条消息
pubnub.objects.setMemberships({
channels: [{ id: channel, custom: { lastReadTimetoken: timetoken } }],
uuid: pubnub.getUserId()
})