メインコンテンツまでスキップ

Manage private chats and chat rooms

Last updated on September 10, 2024

概要

このガイドでは、チャットサービスを使用して、セッションやパーティのチャットルーム内から、プレイヤー間でメッセージを送受信する方法を説明します。パーティは セッション サービスで管理されるため、両者のプロセスはよく似ています。

このガイドでは、このガイドには、トピックという用語が出てきますが、言及される用語のトピックを見ていきます。このガイドにおいては、トピックとチャットルームは同義です。どちらも、チャットメッセージの送信を経由するチャネルを表すものです。

プレイヤーは、セッションに参加するときに自動的にチャットルームに登録され、セッションから抜けるときに削除されます。

目標

このガイドでは、次の方法を理解できます。

  • セッションテンプレートを設定してチャットをサポートする。
  • チャットルーム付きの新しいセッションを作成する。
  • セッションのチャットルームを介してメッセージを送信する。

前提条件

このガイドを読む前に、以下の条件を満たしている必要があります。

  • ロビーセッション、チャットサービスに精通している。
  • ゲームに適した SDK をインストールしている。
  • 管理者ポータルでゲームの 名前空間 にアクセスできる。

セッションのチャットルームの有効化

チャットルームは、セッションレベルで管理されており、セッションテンプレートの一部として有効化できます。チャットを有効化するセッションテンプレートを設定するには、お使いの環境の API ドキュメントを使用する必要があります。手順は次のとおりです。

1.お使いの環境(AccelByte デモ環境など) の Swagger ページを開きます。

1.POST して新しいセッションテンプレートを作成するか、PUT して既存のテンプレートを更新するかを選択します。どちらの場合も、リクエスト本文には必ず適切な値を設定してください。

1.ゲームの名前空間をターゲットにしていることを確認し、textChatを true に設定してリクエスト本文を追加します。

1.リクエストを実行し、出力で、リクエストが正常に実行されたことを確認します。

Sample request Body: 
{
"name": "default",
"deployment": "default",
"inactiveTimeout": 60,
"inviteTimeout": 60,
"joinability": "OPEN",
"maxPlayers": 5,
"minPlayers": 1,
"type": "P2P",
"textChat": "true"
}

チャットサービスへの接続

プレイヤーはチャットメッセージを送受信する前に、まず AccelByte アイデンティティサービスの認証を受ける必要があります。これにより、プレイヤーは自動的にそのユーザーアカウントでチャットサービスに接続されます。

OSS プラグインを使用している場合は、DefaultEngine.ini で次の設定値を有効化して、ユーザーがログイン後、チャットサービスに自動で接続できるようにする必要があります。

[OnlineSubsystemAccelByte]
bAutoChatConnectAfterLoginSuccess=true

セッションのチャットルームの作成

チャットが有効になっているテンプレートを使用して作成されたセッションの場合は通常、追加手順なしでセッションのチャットルームを作成できます。当社のサービスを使用してチャットルーム付きのセッションを作成する方法は、3 つあります。

1.マッチメイキング を使用します。マッチが見つかったときに、ゲームセッションがセッションテンプレートに基づいて作成されます。テンプレートでチャットが有効である場合、セッションのチャットルームが自動的に作成されます。

1.手動でセッションテンプレートを使用してセッションを作成します。テンプレートでチャットが有効である限り、セッションのチャットルームは自動的に作成されます。

1.セッションテンプレートを使用せずにセッションを作成します。これには、チャットを true に設定するなど、必要なセッション設定を渡す必要があります。

テンプレートを使用せずにセッションを作成する必要がある場合、次のコードサンプルを使用できます。チャットが有効なCreateSessionを呼び出す前にセッション設定を定義する方法を確認できます。

// This example illustrates how to manually create a party session with a chat room / topic.
FAccelByteModelsV2PartyCreateRequest CreatePartyRequest;

// Set text chat as true or the session.
CreatePartyRequest.TextChat = true;

// Bind this delegate to listen for the player being added to the session chat room / topic.
ApiClient->Chat.SetAddToTopicNotifDelegate(
Api::Chat::FAddToTopicNotif::CreateLambda([](const FAccelByteModelsChatUpdateUserTopicNotif& Result)
{
UE_LOG(LogTemp, Log, TEXT("Added to topic [%s] by user [%s]"), *Result.TopicId, *Result.SenderId);
})
);

// Create the new session with a chat room / topic.
ApiClient->Session.CreateParty(CreatePartyRequest,
THandler<FAccelByteModelsV2PartySession>::CreateLambda(
[](const FAccelByteModelsV2PartySession& Result)
{
UE_LOG(LogTemp, Log, TEXT("Party created"));
}),
FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
UE_LOG(LogTemp, Error, TEXT("Error code: %d\nError message:%s"), ErrorCode, *ErrorMessage);
})
);
注記

返される ID (RoomId/TopicId)には、作成されたセッションのタイプに基づくプレフィックスが付きます。

  • s.は、V2 ゲームセッションのチャットルーム/トピックに付きます。
  • p.は、V2 パーティセッションのチャットルーム/トピックに付きます。

セッションのチャットルームへの参加

プレイヤーがセッションに参加すると、追加作業なしで自動的にチャットルームにメンバーとして登録されます。

適切なデリゲートを登録すると、他のプレイヤーがチャットルームに追加されるのをリッスンできます。

// Bind this delegate to listen for the player being added to a session chat room / topic.
ApiClient->Chat.SetAddToTopicNotifDelegate(
Api::Chat::FAddRemoveFromTopicNotif::CreateLambda([](const FAccelByteModelsChatUpdateUserTopicNotif& Result)
{
UE_LOG(LogTemp, Log, TEXT("Added to topic [%s] by user [%s]"), *Result.TopicId, *Result.SenderId);
})
);

セッションのチャットルームからの退室

プレイヤーがセッションから抜ける場合や、セッションからキックアウトされる場合、セッションサービスが、チャットルームのメンバーの削除を管理します。

適切なデリゲートを登録すると、他のプレイヤーがチャットから削除されるのをリッスンできます。

// Bind this delegate to listen for the player being removed to a session chat room / topic.
ApiClient->Chat.SetRemoveFromTopicNotifDelegate(
Api::Chat::FAddRemoveFromTopicNotif::CreateLambda([](const FAccelByteModelsChatUpdateUserTopicNotif& Result)
{
UE_LOG(LogTemp, Log, TEXT("Removed from topic [%s] by user [%s]"), *Result.TopicId, *Result.SenderId);
})
);

セッションでのチャットメッセージの送信

プレイヤーはチャットルームに入った後、ルーム内の他のプレイヤーが受け取れるメッセージの送信を開始できます。プレイヤーは複数のチャットルームに登録可能であるため、メッセージの送信をリクエストする際には、メッセージを確実に正しいセッションのチャットルームに届けられるよう、適切なルーム ID/トピック ID を入力する必要があります。

FApiClientPtr ApiClient = FMultiRegistry::GetApiClient(TEXT("0"));

// Call SendChat from the Chat Interface to send the message.
// You will need to have acquired and stored the ID when the player joins the session chat room / topic.
ApiClient->Chat.SendChat(TEXT("topic_id"), TEXT("message to send"), Api::Chat::FSendChatResponse::CreateLambda(
[](const FAccelByteModelsChatSendChatResponse& Result)
{
UE_LOG(LogTemp, Log, TEXT("Send message to topic success."));
}),
FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
UE_LOG(LogTemp, Error, TEXT("Error code: %d\nError message:%s"), ErrorCode, *ErrorMessage);
})
);

セッションでのチャットメッセージの受信

他のプレイヤーからのチャットメッセージをリッスンするため、適切なデリゲートを登録します。関連する送信先ルーム ID/トピック ID と共に新しいメッセージが受信されると、そのデリゲートが呼び出されます。

// Bind this delegate to listen for new messages sent from other players.
ApiClient->Chat.SetChatNotifDelegate(
Api::Chat::FChatNotif::CreateLambda([](const FAccelByteModelsChatNotif& Result)
{
UE_LOG(LogTemp, Log, TEXT("Receive chat! From[%s] content: [%s]"), *Result.From, *Result.Message);
})
);

Retrieve chat history

The Chat service stores messages in the chat history, which can be accessed by moderators for review or by users to view their own messages. The chat history is managed as follows:

  • Messages are organized by topic ID, making it easy to retrieve them based on their topic.
  • Messages are recorded every second and are available for retrieval after a 1-second delay.
  • By default, chat history is kept for 30 days and is then automatically deleted.
  • Users can only access chat history for topics they are a member of.

To retrieve chat history, use the queryChat WebSocket command with the following parameters:

  • topicId: The ID of the topic from which you want to retrieve messages.
  • limit: The maximum number of messages to retrieve.
  • lastChatCreatedAt: The earliest timestamp of messages to retrieve, specified in Unix time.