Presence 基础
Presence 服务让你具备洞察指定频道中用户状态的能力:
- 获取频道内在线用户列表
- 获取在线用户所在频道列表
- 动态监控在线用户临时状态变更
获取频道内在线用户
当用户进入一个新的频道时,往往需要知道当前频道中哪些用户在线,并获取在线用户列表等。你可以通过 WhoNowAsync
方法实现此功能。根据你的参数设置,该方法可以返回当前频道在线用户的用户 ID 列表及其临时状态数据或者仅返回当前频道在线用户的数量等。
该方法对 Message Channel 和 Stream Channel 都适用,你可以使用 channelType
参数加以区分。
获取到这些初始信息后,你可以通过 OnPresenceEvent
事件通知实时更新频道中在线用户信息。
下面是查询频道内在线用户列表及其临时状态的示例代码:
WhoNowAsync
方法一次返回一页数据,一页数据最多包含 100 个在线用户信息,如果频道在线用户超过 100 人,则会在返回结果中的 NextPage
字段包含下一页数据的书签。你需要在每次查询后判断 response.NextPage
是否为空,从而判断是否还有下一页数据。查询下一页数据时,将 response.NextPage
的值填入 PresenceOptions
参数的 page
字段即可实现分页查询,以此往复,直到 response.NextPage
的值为空便是最后一页。例如:
var options = new PresenceOptions();
options.withState = true;
options.withUserId = true;
options.page = "Next_Page_Bookmark";
var (status,response) = await rtmClient.GetPresence().WhoNowAsync("Chat_room", RTM_CHANNEL_TYPE.MESSAGE, options);
...
当频道中人数众多时,你可能只关心频道中在线用户数量,而不关心他们是谁及其临时状态如何。此时可以设置 PresenceOptions
中的 withState
和 withUserId
字段为 false
。例如:
var options = new PresenceOptions();
options.withState = false;
options.withUserId = false;
var (status,response) = await rtmClient.GetPresence().WhoNowAsync("Chat_room", RTM_CHANNEL_TYPE.MESSAGE, options);
if (status.Error)
{
Debug.Log(string.Format("{0} is failed!}", status.Operation) );
Debug.Log(string.Format("Error code : {0}", status.ErrorCode));
Debug.Log(string.Format("Due to: {0}", status.Reason));
}
else
{
Debug.Log(string.Format("{0} users online ", response.totalOccupancy));
}
此时返回值 response
中只有 totalOccupancy
字段有效,表示当前频道总人数,而其他字段全部为空。
不可以同时设置 withState = true
且 withUserId = false
,即获取用户的临时状态就必须要返回用户 ID,否则会设置失败并收到错误码。
获取在线用户所在频道
你可能需要查询某个用户当前在哪些频道中,例如订阅了哪些 Message Channel 或加入了哪些 Stream Channel。特别是作为 app 管理员想要追踪用户路径时,这个功能就显得特别重要,此时你可以调用 WhereNowAsync
方法来实现。
var (status,response) = await rtmClient.GetPresence().WhereNowAsync("Tony");
if (status.Error)
{
Debug.Log(string.Format("{0} is failed!}", status.Operation));
Debug.Log(string.Format("Error code : {0}", status.ErrorCode));
Debug.Log(string.Format("Due to: {0}", status.Reason));
}
else
{
Debug.Log(string.Format("User Tony is now in {0} channels ", response.Count));
if (response.Channels.Length > 0)
{
foreach (ChannelInfo channel in response.Channels)
{
Debug.Log(string.Format("Channel Name: {0}, Channel Type:{1}", channel.channelName, channel.channelType));
}
}
}
WhereNowAsync
方法会返回查询用户所在频道及其类型的信息。该方法会一次性返回所有查询结果,不会进行分页。