跳转到主要内容

频道列表 查询

频道列表查询。

WebSocket 方式

通过WebSocket查询当前可访问的频道列表。

方法 信息

内容
方法AChatManager::GetInstance()->GetChannels()
回调OnChannels(const TArray<FChatChannelInfo>& Channels)

FChatChannelInfo

字段类型说明
channelFString频道名称
countint32在线人数

代码示例

void UMyGame::GetChannels()
{
AChatManager::GetInstance()->GetChannels();
}

void UMyGame::OnChannels(const TArray<FChatChannelInfo>& Channels)
{
for (const auto& Channel : Channels)
{
UE_LOG(LogTemp, Log, TEXT("Channel: %s, Users: %d"), *Channel.channel, Channel.count);
}
}

REST API 方式

Socket 连接 前使用 可以

REST API方式可在WebSocket连接前使用。如果想在聊天服务器连接前预先查询频道列表,可以使用此方式。

URL确认

此API使用 service-api.playnanoo.com 域名。

API信息

  • URL: https://service-api.playnanoo.com/chat/v20221201/channels
  • Method: GET
  • 需要认证: 否

请求参数

没有额外参数。

响应数据

字段类型说明
Itemsarray频道列表
Items[].channelstring频道名称
Items[].countint在线人数

代码示例

void UMyGame::GetChannelList()
{
TSharedRef<IHttpRequest> Request = FHttpModule::Get().CreateRequest();
Request->SetURL(TEXT("https://service-api.playnanoo.com/chat/v20221201/channels"));
Request->SetVerb(TEXT("GET"));
FPlayNANOOHelper::SetCommonHeaders(Request, false); // 인증 토큰 불필요

Request->OnProcessRequestComplete().BindLambda(
[](FHttpRequestPtr Req, FHttpResponsePtr Res, bool bSuccess)
{
if (bSuccess && Res.IsValid())
{
TSharedPtr<FJsonObject> JsonObject;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Res->GetContentAsString());

if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
const TArray<TSharedPtr<FJsonValue>>* Items;
if (JsonObject->TryGetArrayField(TEXT("Items"), Items))
{
for (const auto& Item : *Items)
{
TSharedPtr<FJsonObject> ChannelObj = Item->AsObject();
FString Channel = ChannelObj->GetStringField(TEXT("channel"));
int32 Count = ChannelObj->GetIntegerField(TEXT("count"));
UE_LOG(LogTemp, Log, TEXT("Channel: %s, Count: %d"), *Channel, Count);
}
}
}
}
});

Request->ProcessRequest();
}