获取社交关系
注意
本主题演示高级 API 使用情况。 如果在社交管理器中发现不支持的方案,请让开发人员客户经理 (DAM) 知道。
本主题提供一个代码示例,演示如何使用 Social Manager API 检索用户的社交关系及其公共属性。
若要开始使用社交管理器,请参阅 社交管理器概述。 社交管理器 API 显著简化了跟踪在线好友及其游戏活动的开发。
获取第一个用户的社交关系
下面的代码示例演示如何检索与 Xbox 服务的社交关系。
代码示例执行以下操作:
- 生成系统上所有用户的列表,然后检索第一个用户。
- 检索该用户的所有社交关系。
- 显示这些关系中每个关系的公共属性。
平面 C API
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
HRESULT hr = XblSocialGetSocialRelationshipsResult(asyncBlock, &state.socialResultHandle);
// Be sure to call XblSocialRelationshipResultCloseHandle when the result object is no longer needed.
};
HRESULT hr = XblSocialGetSocialRelationshipsAsync(
xboxLiveContext,
xboxUserId,
socialRelationshipFilter,
0,
0,
asyncBlock.get()
);
if (SUCCEEDED(hr))
{
// The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
// If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
asyncBlock.release();
}
有关详细信息,请参阅以下内容:
- XAsyncBlock
- XblSocialGetSocialRelationshipsAsync
- XblSocialGetSocialRelationshipsResult
- XblSocialRelationshipResultCloseHandle
获取单个关系
平面 C API
const XblSocialRelationship* relationships = nullptr;
size_t relationshipsCount = 0;
HRESULT hr = XblSocialRelationshipResultGetRelationships(state.socialResultHandle, &relationships, &relationshipsCount);
LogToFile("Got %u SocialRelationships:", relationshipsCount);
for (size_t i = 0; i < relationshipsCount; ++i)
{
LogToFile("Xuid = %u, isFollowingCaller = %u", relationships[i].xboxUserId, relationships[i].isFollowingCaller);
}
有关详细信息,请参阅以下内容:
获取下一页关系
平面 C API
bool hasNext{ false };
HRESULT hr = XblSocialRelationshipResultHasNext(state.socialResultHandle, &hasNext);
if (hasNext)
{
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->context = nullptr;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of XAsyncBlock*.
// Close the handle to the previous page of results.
if (state.socialResultHandle)
{
XblSocialRelationshipResultCloseHandle(state.socialResultHandle);
}
HRESULT hr = XblSocialRelationshipResultGetNextResult(asyncBlock, &state.socialResultHandle);
};
uint32_t maxItems = 100;
HRESULT hr = XblSocialRelationshipResultGetNextAsync(xboxLiveContext, state.socialResultHandle, maxItems, asyncBlock.get());
if (SUCCEEDED(hr))
{
// The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
// If the call fails, std::unique_ptr will keep ownership and delete XAsyncBlock*.
asyncBlock.release();
}
}
有关详细信息,请参阅以下内容: