频道¶
创建频道¶
要创建聊天频道,请创建CreateChannelParams对象,然后调用Chat类的createChannel()方法。
CreateChannelParams¶
| 字段名 | 说明 | 类型 | 必填 |
|---|---|---|---|
| channelId | 频道 ID (可使用英文字母大小写、数字以及部分特殊字符( -, ., _, ~, :),最多 100 个字符) | string | Y |
| password | 密码(仅 PRIVATE 频道时为必填字段)(最多 50 个字符) | string | N |
| channelName | 频道名称 (最多 50 个字符) | string | Y |
| maxMemberCount | 频道最大参与人数 (最少 2 人,最多 5,000 人) (仅在不是 ONE_ON_ONE 频道时为必填) | integer | N |
| type | 频道类型 (PRIVATE, PUBLIC, GROUP, ONE_ON_ONE) | enum | Y |
| chatHistoryAllowed | 是否允许查询频道历史记录 | boolean | N |
| otherPlayerId | 受邀用户的Player ID(仅ONE_ON_ONE必填) | long | N |
创建ONE_ON_ONE频道时,将type设为ONE_ON_ONE,并在otherPlayerId中填写对方的Player ID。不要传入maxMemberCount。
此示例创建聊天频道。
using hive;
CreateChannelParams createChannelParams = new CreateChannelParams();
createChannelParams.channelId = "CHANNEL_ID";
createChannelParams.password = "";
createChannelParams.channelName = "CHANNEL_NAME";
createChannelParams.maxMemberCount = 8;
createChannelParams.type = ChannelType.PUBLIC;
Chat.createChannel(createChannelParams, (ResultAPI result) => {
if (result.isSuccess()) {
// API 调用成功
}
});
#include "HiveChat.h"
FHiveCreateChannelParams CreateChannelParams = FHiveCreateChannelParams();
CreateChannelParams.ChannelId = TEXT("CHANNEL_ID");
CreateChannelParams.Password = TEXT("");
CreateChannelParams.ChannelName = TEXT("CHANNEL_NAME");
CreateChannelParams.MaxMemberCount = 8;
CreateChannelParams.Type = EHiveChannelType::Public;
FHiveChat::CreateChannel(CreateChannelParams, FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result) {
if (Result.IsSuccess()) {
// API 调用成功
}
});
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
CreateChannelParams param;
param.channelId = "CHANNEL_ID";
param.channelName = "CHANNEL_NAME";
param.password = "";
param.maxMemberCount = 8;
param.type = ChannelType::PUBLIC;
Chat::createChannel(param, [=](ResultAPI const & result) {
if (result.isSuccess()) {
// API 调用成功
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val params = Chat.CreateChannelParams(
channelId = "CHANNEL_ID",
password = "",
channelName = "CHANNEL_NAME",
maxMemberCount = 8,
type = Chat.ChannelType.PUBLIC
)
Chat.createChannel(params, object: Chat.CreateChannelListener{
override fun onResult(result: ResultAPI) {
if (result.isSuccess) {
// API 调用成功
}
}
})
#import "HIVEService.h"
HiveChatCreateChannelParams* params = [[HiveChatCreateChannelParams alloc] init];
params.channelId = @"CHANNEL_ID";
params.password = @"";
params.channelName = @"CHANNEL_NAME";
params.maxMemberCount = 8;
params.type = Chat::ChannelType::PUBLIC;
[HiveChat createChannelWithCreateParams:params handler:^(HIVEResultAPI * result) {
if ([result isSuccess]) {
// API 调用成功
}
}];
删除频道¶
要删除聊天频道,请调用Chat类的deleteChannel()方法。
此示例删除聊天频道。
进入频道¶
要加入聊天频道,请创建EnterChannelParams对象,然后调用Chat类的enterChannel()方法。
只有创建ONE_ON_ONE频道时参与的两个账号可以加入该频道。
EnterChannelParams¶
| 字段名 | 说明 | 类型 | 必填 |
|---|---|---|---|
| channelId | 频道 ID | string | Y |
| password | 密码(PRIVATE 频道时必填) | string | N |
此示例加入聊天频道。
#include "HiveChat.h"
FHiveEnterChannelParams EnterChannelParams = FHiveEnterChannelParams();
EnterChannelParams.ChannelId = TEXT("CHANNEL_ID");
EnterChannelParams.Password = TEXT("");
FHiveChat::EnterChannel(EnterChannelParams, FHiveChatOnResultDelegate::CreateLambda([this](const FHiveResultAPI& Result) {
if (Result.IsSuccess()) {
// API 调用成功
}
}));
退出频道¶
要退出已加入的聊天频道,请调用Chat类的exitChannel()方法。
此示例退出已加入的聊天频道。
查询全部频道列表¶
要查询当前所有频道,请创建GetChannelsParams对象,然后调用Chat类的getChannels()方法。
传入null代替GetChannelsParams对象时,返回当前所有频道,不进行筛选。
要在ONE_ON_ONE频道中不查询频道就向对方发送消息,请调用createChannel()。该方法访问现有频道或创建新频道。
GetChannelsParams¶
| 字段名 | 说明 | 类型 | 必填 |
|---|---|---|---|
| type | 要查询的频道类型 (PRIVATE, PUBLIC, GROUP) | enum | Y |
| channelId | 查询以特定频道 ID 开头的频道 | string | N |
| channelName | 查询包含特定频道名称的频道 | string | N |
| sort | 频道排序基准 (ChannelId, ChannelName, RegTime) | enum | N |
| pageOrder | 排序方式 (ASC, DESC)(默认值 DESC) | string | N |
| pageSize | 每页查询的频道数量 (最少 1 个,最多 10 个,默认值 10) | integer | N |
| pageNumber | 要查询的页码 (从 1 开始,默认值 1) | integer | N |
响应包含Channel和ChannelPage对象,各对象的字段如下。
Channel¶
查询到的频道信息,可查看频道ID、参与人数及是否允许查询消息历史。
| 字段名 | 说明 | 类型 |
|---|---|---|
| channelId | 频道 ID | string |
| type | 频道类型 (PRIVATE, PUBLIC, GROUP) | enum |
| owner | 频道所有者的Hive Player ID | string |
| channelName | 频道名称 | string |
| memberCount | 当前参与成员数 | integer |
| maxMemberCount | 频道最大参与人数 | integer |
| regTime | 频道创建时间(以 UTC+0 为准,格式为 yyyy-MM-dd'T'HH:mm:ss.SSSZ) | string |
| regTimeMillis | 频道创建时间 (Unix Timestamp) | long |
| chatHistoryAllowed | 是否允许查询频道历史记录 | bool |
| query | 查询频道消息历史记录的方法 | method |
ChannelPage¶
频道列表的分页信息,可查看当前页和总页数。
| 字段名 | 说明 | 类型 |
|---|---|---|
| size | 每页项目数 | integer |
| currentPage | 当前页码 | integer |
| totalElements | 全部项目数 | integer |
| totalPages | 总页数 | integer |
注意事项¶
传入null代替GetChannelsParams对象时,返回当前所有频道,不进行筛选。查询对象为PUBLIC、PRIVATE和GROUP频道,不包含ONE_ON_ONE频道。
示例代码¶
此示例查询当前所有PUBLIC类型的频道。
using hive;
GetChannelsParams getChannelsParams = new GetChannelsParams();
getChannelsParams.type = ChannelType.PUBLIC;
Chat.getChannels(getChannelsParams, (ResultAPI result, List<Channel> channels, ChannelPage channelPage) => {
if (result.isSuccess()) {
// API 调用成功
foreach (Channel channel in channels) {
// 获取 Channel
}
if (channelPage != null) {
// 获取 ChannelPage
}
}
});
#include "HiveChat.h"
TOptional<FHiveGetChannelsParams> GetChannelsParams = FHiveGetChannelsParams();
GetChannelsParams->Type = EHiveChannelType::Public;
FHiveChat::GetChannels(GetChannelsParams, FHiveChatOnGetChannelsDelegate::CreateLambda([this](const FHiveResultAPI& Result, const TArray<FHiveChannel>& channels, FHiveChannelPage const & channelPage) {
if (Result.IsSuccess()) {
// API 调用成功
for (const FHiveChannel& Channel : Channels) {
// 获取 Channel
}
if (ChannelPage != nullptr) {
// 获取 ChannelPage
}
}
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
Chat::GetChannelsParams params = Chat::GetChannelsParams();
params.type = Chat::ChannelType::PUBLIC;
Chat::getChannels(param, [=](ResultAPI const & result, std::vector<Channel> const & channels, ChannelPage const & pageInfo) {
if (result.isSuccess()) {
// API 调用成功
for (Chat::Channel channel : channels) {
// 获取 Channel
}
if (channelPage != null) {
// 获取 ChannelPage
}
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val params = Chat.GetChannelsParams(type = Chat.ChannelType.PUBLIC)
Chat.getChannels(params, object: Chat.GetChannelsListener{
override fun onResult(
result: ResultAPI,
channels: ArrayList<Chat.Channel>,
channelPage: Chat.ChannelPage?
) {
if (result.isSuccess) {
// API 调用成功
channels.forEach {
// 获取 Channel
}
channelPage?.let {
// 获取 ChannelPage
}
}
}
})
import com.hive.Chat;
Chat.GetChannelsParams params = new Chat.GetChannelsParams(Chat.ChannelType.PUBLIC, null, null, null, null, null);
Chat.getChannels(params, (result, channels, channelPage) -> {
if (result.isSuccess()) {
// API 调用成功
for (Chat.Channel channel : channels) {
// 获取 Channel
}
if (channelPage != null) {
// 获取 ChannelPage
}
}
});
#import "HIVEService.h"
HiveChatGetChannelsParams* params = [[HiveChatGetChannelsParams alloc] init];
params.type = HiveChatChannelTypePublicChannel;
[HiveChat getChannelsWithGetChannelsParams:params handler:^(HIVEResultAPI * result, NSArray<HiveChatChannelContent *> * channels, HiveChatPageInfo * pageInfo) {
if ([result isSuccess]) {
// API 调用成功
for (Chat::Channel *channel : channels) {
// 获取 Channel
}
if (pageInfo != pageInfo) {
// 获取 ChannelPage
}
}
}];
查询频道信息¶
要查询特定频道的详细信息,请调用Chat类的getChannelInfo()方法。
响应包含Channel和Member对象,各对象的字段如下。
Channel¶
查询到的频道信息,可查看频道ID、参与人数及是否允许查询消息历史。
| 字段名 | 说明 | 类型 |
|---|---|---|
| channelId | 频道 ID | string |
| type | 频道类型 (PRIVATE, PUBLIC, GROUP) | enum |
| owner | 频道所有者的Hive Player ID | string |
| channelName | 频道名称 | string |
| memberCount | 当前参与成员数 | integer |
| maxMemberCount | 频道最大参与人数 | integer |
| regTime | 频道创建时间(以 UTC+0 为准,格式为 yyyy-MM-dd'T'HH:mm:ss.SSSZ) | string |
| regTimeMillis | 频道创建时间 (Unix Timestamp) | long |
| chatHistoryAllowed | 是否允许查询频道历史记录 | bool |
| query | 查询频道消息历史记录的方法 | method |
Member¶
频道成员信息,可查看Player ID和连接时间。
| 字段名 | 说明 | 类型 |
|---|---|---|
| playerId | Hive Player ID | long |
| connectedTime | 连接时间(以 UTC+0 为准,格式为 yyyy-MM-dd'T'HH:mm:ss.SSSZ) | string |
| connectedTimeMillis | 连接时间 (Unix Timestamp) | long |
| 此示例查询特定频道的详细信息。 |
#include "HiveChat.h"
FString ChannelId = TEXT("CHANNEL_ID");
FHiveChat::GetChannelInfo(ChannelId, FHiveChatOnGetChannelInfoDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FHiveChannel& channel, const TArray<FHiveMember>& members) {
if (Result.IsSuccess()) {
// API 调用成功
if (channel != nullptr) {
// 获取 Channel
}
for (const FHiveMember& Member : Members) {
// 获取 Member
}
}
}));
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
std::string channelId = "CHANNEL_ID";
Chat::getChannelInfo(channelId, [=](ResultAPI const & result, Channel const & channel, std::vector<Member> const & members) {
if (result.isSuccess()) {
if (channel != null) {
// 获取 Channel
}
for (Chat::Member member : members) {
// 获取 Member
}
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val channelId = "CHANNEL_ID"
Chat.getChannelInfo(channelId, object: Chat.GetChannelInfoListener {
override fun onResult(
result: ResultAPI,
channel: Chat.Channel?,
members: ArrayList<Chat.Member>
) {
if (result.isSuccess) {
channel?.let {
// 获取 Channel
}
members.forEach {
// 获取 Member
}
}
}
})
#import "HIVEService.h"
NSString *channelId = @"CHANNEL_ID";
[HiveChat getChannelInfoWithChannelId:channelId handler:^(HIVEResultAPI * result, HiveChatChannelContent * channel, NSArray<HiveChatMember *> * members) {
if ([result isSuccess]) {
if (channel != null) {
// 获取 Channel
}
for (Chat::Member *member : members) {
// 获取 Member
}
}
}];
查询频道参与成员¶
要查询频道成员,请调用Chat类的getChannelMembers()方法。 返回值与getChannelInfo()传递的Member对象相同。
#include "HiveChat.h"
FString ChannelId = TEXT("CHANNEL_ID");
FHiveChat::GetChannelMembers(ChannelId, FHiveChatOnGetChannelMembersDelegate::CreateLambda([this](const FHiveResultAPI& Result, const TArray<FHiveMember>& members) {
if (Result.IsSuccess()) {
for (const FHiveMember& Member : members) {
// 获取 Member
}
}
}));
查询频道消息历史¶
调用channel.query(ChannelMessageListQueryParams, ChannelMessageListQueryListener)以加载历史聊天记录。
仅可查询chatHistoryAllowed属性为true的频道的消息历史。
ChannelMessageListQueryParams¶
| 字段名 | 说明 | 类型 | 必填 |
|---|---|---|---|
| prevSize | 查询之前消息的数量(最少 0,最多 50,默认值 0) | integer | N |
| nextSize | 查询之后消息的数量(最少 0,最多 50,默认值 0) | integer | N |
| messageId | 以该消息 ID 为基准查询消息记录。如果该值为 null,则以最新消息为基准进行查询。 | string | N |
| sort | 默认按日期降序排序,可根据查询方向调整排序方式(DESC or ASC),默认值 DESC | string | N |
ChannelMessageListQueryResponse¶
消息历史查询结果,可查看查询到的消息以及是否可继续查询之前或之后的历史。
| 字段名 | 说明 | 类型 |
|---|---|---|
| hasNext | 是否存在之后可继续查询的消息 | boolean |
| nextMessageId | 继续查询之后消息时作为基准的消息 ID | string |
| hasPrev | 是否存在之前可继续查询的消息 | boolean |
| prevMessageId | 继续查询之前消息时作为基准的消息 ID | string |
| content | 查询到的消息结果 | list<ChannelMessage> |
在查询结果的每个ChannelMessage中,通过messageReaction查看消息的反应统计。服务器简化统计时,simplified为true,各反应编号的用户列表playerIds为空。此时用户列表为空并不代表没有反应。
要查看详细信息或自己的反应状态,请参阅查询频道消息反应。
此示例以特定消息ID为基准,查询之前和之后各10条消息。
using hive;
ChannelMessageListQueryParams queryParams = new ChannelMessageListQueryParams();
queryParams.prevSize = 10;
queryParams.nextSize = 10;
queryParams.messageId = "TARGET_MESSAGE_ID";
channel.query(queryParams, (ResultAPI result, ChannelMessageListQueryResponse response) => {
if (result.isSuccess() && response != null) {
foreach (ChannelMessage message in response.content) {
// 频道消息列表
}
}
});
#include <HIVE_SDK_Plugin/HIVE_CPP.h>
using namespace hive;
ChannelMessageListQueryParams queryParams;
queryParams.prevSize = 10;
queryParams.nextSize = 10;
queryParams.messageId = "TARGET_MESSAGE_ID";
channel.query(queryParams, [=](ResultAPI const & result, ChannelMessageListQueryResponse const & response) {
if (result.isSuccess()) {
for (auto message : response.message) {
// 频道消息列表
}
}
});
import com.hive.Chat;
import com.hive.ResultAPI;
val queryParams = Chat.ChannelMessageListQueryParams(
prevSize = 10,
nextSize = 10,
messageId = "TARGET_MESSAGE_ID"
)
channel.query(queryParams, object : Chat.ChannelMessageListQueryListener {
override fun onResult(
result: ResultAPI,
response: Chat.ChannelMessageListQueryResponse?
) {
if(result.isSuccess && response != null) {
response.content.forEach {
// 频道消息列表
}
}
}
})
import com.hive.Chat;
Chat.ChannelMessageListQueryParams queryParams = new Chat.ChannelMessageListQueryParams();
queryParams.prevSize = 10;
queryParams.nextSize = 10;
queryParams.messageId = "TARGET_MESSAGE_ID";
channel.query(queryParams, (result, response) -> {
if (result.isSuccess() && response != null) {
for (HiveChatChannelMessage message : response.getContent()) {
// 频道消息列表
}
}
});
#import "HIVEService.h"
HiveChatChannelMessageListQueryParams* queryParams = [HiveChatChannelMessageListQueryParams new];
queryParams.prevSize = 10;
queryParams.nextSize = 10;
queryParams.messageId = @"TARGET_MESSAGE_ID";
[channel queryWithParams:queryParams listener:^(HIVEResultAPI * result, HiveChatChannelMessageListQueryResponse* response) {
if ([result isSuccess] && response.content) {
for (HiveChatChannelMessage *message in response.content) {
// 频道消息列表
}
}
}];
设置频道自动翻译接收¶
设置是否接收频道消息的自动翻译。请在收到的ChannelMessage对象中查看翻译结果。
频道事件管理¶
Hive检测聊天Socket服务器连接状态,持续向应用传递用户和频道的状态变化事件。处理方法请参阅事件管理 > 频道事件。