# 2. テストクライアントで疎通確認する

{% hint style="info" %}
こちらは 1. のチュートリアルが完了していることが前提となります。
{% endhint %}

## はじめに

[Diarkis サーバーテンプレート](/getting-started/diarkis-server-template.md) には、コマンドをテストするためのテストクライアントが用意されています。このテストクライアントは Go で書かれています。

これを使って、基本的なビルトインコマンドを発行し、疎通確認することができます。

## テストクライアントのビルド

テストクライアントのバイナリは、Diarkis サーバーテンプレートでビルドすることができます。

前回のチュートリアルで `make build-local` を実行した際に合わせてテストクライアントがビルドされるので、それを利用します。

## Diarkis テストクライアントの起動

以下の make タスクを実行します。

```bash
$ make go-cli host=127.0.0.1:7000 uid=test1
:
[UID: test1][SID(UDP): b674cd4a79594581b90186818c1ef911]
 > Connected UDP
```

* host: Diarkis サーバーのエンドポイント
* uid: 接続するユーザーの ID
* clientKey: クライアントキー（デフォルトでは無効になっているので今回は不要です）

## ms (MARS Stats) ツール

Diarkis の現在の情報を取得できるツールです。

現在の CCU やパケット数、Room 数など分析に役立つ情報を取得できます。

```
# ./remote_bin/ms {MARS address:port} {no-color}
$ watch ./remote_bin/ms 127.0.0.1:6779 no-color
─────────────────────────────────────────────────────────────────────────────────────────── 1.1.0 ───────────────────────────────────────────────────────────────────────────────────────────────────
ADDRESS                    PUBLIC-ADDRESS   STATUS   STARTED                            CCU   MESH-IN   MESH-OUT   UDP-IN   UDP-OUT   TCP-IN   TCP-OUT   MM-SEARCH   ROOMS   P2P-ATTEMPTS   P2P-SUCCESS
HTTP/HTTP/127.0.0.1:8100   127.0.0.1:7000   ONLINE   2024-10-04T17:58:39+09:00[16min]   0     2         2          0        0         0        0         0           0       0              0
UDP/UDP/127.0.0.1:8101     127.0.0.1:7100   ONLINE   2024-10-04T17:58:40+09:00[16min]   1     2         2          0        0         0        0         0           0       0              0
```

***

## Room を作成・参加する

テストクライアントを 2 つ起動して Room を作成・参加してみましょう。

一方のクライアントで  `room create` コマンドを発行して Room を作成したあとに、他方のクライアントで `room join` コマンドを発行して Room に参加してみましょう。

### uid: test1 で Room を作成する

```bash
> room create
Which client to create a room? [tcp/udp] > udp
[UID: test1][SID(UDP): xxxx][RoomID: yyyy]
> New member joined room - Hello from test2
```

### uid: test2 で Room に参加する

```textile
> room join
Which client to join a room? [tcp/udp] udp
Type room ID > yyyy
handleOnJoinRoom
UDP Room yyyy joined - success true and it was created at 1728033877
New member joined room - Hello from test2
[UID: test2][SID(UDP): zzzz][RoomID: yyyy]
```

***

## ms で CCU と Room 数を確認する

テストクライアントを2つ起動してRoom に参加したことで、CCU と Room 数が増えていることが確認できます。

```
$ watch ./remote_bin/ms 127.0.0.1:6779 no-color
────────────────────────────── 1.1.0 ─────────────────────────────────
ADDRESS                    PUBLIC-ADDRESS   STATUS   CCU ... ROOMS ...
HTTP/HTTP/127.0.0.1:8100   127.0.0.1:7000   ONLINE   0   ...   0   ...
UDP/UDP/127.0.0.1:8101     127.0.0.1:7100   ONLINE   2   ...   1   ...
```

***

## Room メンバー全員にメッセージを送信する

`room broadcast` コマンドを発行し、任意のメッセージを送ることができます。

`room +` というショートハンドでも同様のことが可能です。

### uid: test1 で broadcast する

```
> room broadcast
Which client to broadcast to a room? [tcp/udp] udp
Type room ID: yyyy
Type message: Hello, Diarkis!!
Room broadcast - Hello, Diarkis!!
[UID: test1][SID(UDP): xxxx][RoomID: yyyy]
```

### uid: test2 でメッセージを受信

```
[UID: test2][SID(UDP): zzzz][RoomID: yyyy]
 > Room broadcast - Hello, Diarkis!!
```

***

## Room の任意のメンバーにメッセージを送信する

`room message` コマンドを発行し、任意のメッセージを送ることができます。

### uid: test1 から test2 に送信する

```
> room message
Which client to message to a room? [tcp/udp] udp
Type user ID: test2
Type message: nice!
[UID: test1][SID(UDP): xxxx][RoomID: yyyy]
```

### uid: test2 でメッセージを受信

```
[UID: test2][SID(UDP): zzzz][RoomID: yyyy]
 > Room message - nice!
```

***

## Room から退出する

`room leave` コマンドを発行し、Room を退出することができます。

```
[UID: test1][SID(UDP): xxxx][RoomID: yyyy]
> room leave
Which client to leave a room? [tcp/udp]
udp
Type room ID:
yyyy
Room left. success:true
[UID: test1][SID(UDP): xxxx]
```

***

## テストクライアントのコマンド確認

`help` コマンドで、利用できるコマンドの一覧が確認できます。

`help room` のように引数にモジュール名をいれることで、特定のモジュールに絞って確認もできます。

```
> help
================ Command List ================
help                    - Display the list of valid commands
help {module name}      - Display the list of valid commands for the module
reconnect               - Reconnects to another server
disconnect              - Disconnects
ph                      - Sends a TCP Hey
h                       - Sends a UDP Hello
rh                      - Sends an RUDP Hello
die                     - Ungraceful disconnect from the server
:
=============================================
 > help room
================ Command List ================
room create             - Creates a room
room join               - Joins a room
room join random        - Joins a random room or creates a new room
room leave              - Leaves a room
room get owner          - Get a room owner id
room get members        - Get a room member ids
room get num            - Get a number of room members
room migrate            - Migrates a room to another server
room message            - Sends a message to one selected member of the room
room broadcast          - Reliable broadcast to a room
room +                  - Alias for `room broadcast`
room ubroadcast         - Unreliable broadcast to a room
room -                  - Alias for `room ubroadcast`
room p2p                - Starts P2P with room members
room relay              - Sends a relay message to other room members
room relay to           - Sends a relay message to selected room members
room relay profile      - Sends a relay profile
room relay to profile   - Sends a relay profile to selected room members
room props update       - Update room property
room props get          - Get room property
room props incr         - Increment room property
room props sync         - Sync room property
room reserve            - Reserves a room
room cancel reservation - Cancel a room reservation
room register           - Registers a room with a type
room find               - Finds rooms by type
room obj incr           - Increment room objects
room obj delete         - Delete room objects
room obj update         - Update room objects
room chat               - Chat in a room
room chat log           - Get chat log in a room
=============================================
```


---

# Agent Instructions: 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:

```
GET https://help.diarkis.io/getting-started/tutorial/test-client.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
