> For the complete documentation index, see [llms.txt](https://help.diarkis.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.diarkis.io/diarkis-client/samples/cpp/room_chat.md).

# room\_chat

### room\_chat サンプル

#### 概要

room\_chat サンプルは、Diarkis サーバーの Room Chat 機能を使用したインタラクティブな CLI サンプル・プログラムです。ユーザーは Room に参加し、リアルタイムでカスタムチャットメッセージの送受信や、サーバーへの同期コマンドでチャット履歴の取得を行うことができます。

room\_chat サンプルでは以下の機能を確認することができます。

* Room の参加
* Room メンバーへのカスタムチャットメッセージ送信
* チャット履歴の取得（同期コマンド）
* Room からの退室

#### ローカル環境でサーバーを起動する

サンプルで使用するサーバーを起動するためのチュートリアルを実施して、ローカル環境で Diarkis サーバーを起動します。

1. [Diarkis サーバーをローカル環境で起動する](/getting-started/tutorial/setup-local-server.md)

#### サンプルの引数

サンプルの起動時には以下の３つのパラメータを指定してください。

| 引数         | 説明                            |   |
| ---------- | ----------------------------- | - |
| serverAddr | Diarkis サーバのエンドポイントを指定してください  |   |
| UID        | 接続するユーザーの ID を任意の文字列で指定してください |   |
| clientKey  | クライアントキーを任意の文字列で指定してください      |   |

起動例：

<pre><code><strong>room_chat.exe 192.168.1.123:7000 1111 AAAA
</strong></code></pre>

#### room\_chat サンプル解説

Diarkis ランタイムおよび Diarkis Module を初期化し、Diarkis サーバーへ接続します。詳細については [Diarkis モジュール利用の全体的な流れ](/diarkis-client/diarkis-module.md) を参照してください。

Room モジュールをセットアップします。

```
// Room モジュールのセットアップ
diarkis->SetupRoom(false);
```

RandomJoin で Room に入室します。DiarkisRoomBase::RandomJoinRoom() は参加可能な Room が存在すればその Room に参加し、存在しなければ新たに Room を作成します。サーバーへリクエストを送信後、DiarkisRoomBase::IsJoin() で Room 入室完了を待機します。

Room 入室後、サンプルはインタラクティブなコマンドループに入ります。標準入力から以下のコマンドで操作できます。

* /send/<メッセージ> — Room にチャットメッセージを送信する
* /sync — サーバーから全チャット履歴を取得する。OnRoomChatLog コールバックで結果が表示される
* /quit — Room を退室してプログラムを終了する
* /help — 使用可能なコマンドを表示する

DiarkisRoomBase::SendLeaveRoom() で Room からの退室をリクエストします。DiarkisRoomBase::IsLeave() で退室完了を待機します。

終了処理

[Diarkis モジュール利用の全体的な流れ](/diarkis-client/diarkis-module.md) を参照してください。

#### 注意点

* Windows 環境では、コンソールを UTF-8 エンコーディングに設定して日本語などマルチバイト文字のチャットメッセージを正しく処理します。
* 複数のクライアントを別々のターミナルウィンドウから同時に起動して、Room Chat の動作を確認できます。
* /sync コマンドはサーバーからチャット履歴を取得します。結果は OnRoomChatLog コールバックで表示されます。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.diarkis.io/diarkis-client/samples/cpp/room_chat.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
