# イベント

## イベント <a href="#undefined" id="undefined"></a>

Game Chatでは、クライアント側で発生する様々なイベントを処理できるイベントリスナー(Event Listener)機能を提供します。この機能により、ユーザーはチャットアプリケーション内で起こる様々な状況をリアルタイムで監視し、適切に反応することができます。以下は、主なイベントとイベント処理方法についての説明です。

## 主なイベントタイプ <a href="#undefined" id="undefined"></a>

1. **メッセージ受信** : 新しいメッセージを受信したときにトリガーされます。
2. **メッセージ削除** : メッセージが削除されたときにトリガーされます。
3. **エラーメッセージ** : エラーが発生したときにトリガーされます。
4. **アクセス成功** : サーバへのアクセスが成功したときにトリガーされます。
5. **アクセス終了** : サーバアクセスが終了したときにトリガーされます。
6. **タイピング開始/終了** : ユーザーがタイピングを開始または終了したときにそれぞれトリガーされます。
7. **メンバー追加/削除** : チャンネルにユーザーが追加または削除されたときにトリガーされます。
8. **メンバー停止/退会** : ユーザーがチャンネルから停止されたり退会したときにトリガーされます。

次は、クライアント側からイベントを受信する方法です。

```csharp
nc.dispatcher.onMessageReceived += message =>
{
    Console.WriteLine("received a new message: ", message);
}
```

## イベントハンドラ接続と解除 <a href="#undefined" id="undefined"></a>

イベントハンドラを使用して様々なイベントを受信し、必要なロジックを実装できます。以下のコードは、各イベントに対するイベントハンドラの接続と解除方法を示しています。

```csharp
// メッセージ受信
nc.dispatcher.onMessageReceived += e =>
{
    Console.WriteLine("onMessageReceived: ", e);
};

// メッセージ削除
nc.dispatcher.onMessageDeleted += e =>
{
    Console.WriteLine("onMessageDeleted: ", e);
};

// エラーメッセージ
nc.dispatcher.onErrorReceived += e =>
{
    Console.WriteLine("[CloudChatSample] onErrorReceived: ", e);
};

// アクセス成功
nc.dispatcher.onConnected += e =>
{
    Console.WriteLine("[CloudChatSample] Connected to server with id: {0} ", e);
};

// アクセス終了
nc.dispatcher.onDisconnected += e =>
{
    Console.WriteLine("Disconnected");
};

// タイピングを開始する場合
nc.dispatcher.onStartTyping += e =>
{
    Console.WriteLine("onStartTyping: ", e);
};

// タイピングを終了する場合
nc.dispatcher.onStopTyping += e =>
{
    Console.WriteLine("onStopTyping: ", e);
};

// ユーザーがチャンネルを登録した場合
nc.dispatcher.onMemberAdded += e =>
{
    Console.WriteLine("onMemberAdded: ", e);
};

// ユーザーがチャンネル登録を解除した場合
nc.dispatcher.onMemberLeft += e =>
{
    Console.WriteLine("[CloudChatSample] onMemberLeft: ", e);
};

// チャンネルからユーザーが停止された場合
nc.dispatcher.onMemberBanned += e =>
{
    Console.WriteLine("[CloudChatSample] onMemberBanned: ", e);
};

// チャンネルからユーザーが退会した場合
nc.dispatcher.onMemberDeleted += e =>
{
    Console.WriteLine("[CloudChatSample] onMemberDeleted: ", e);
};

nc.dispatcher.onSubscriptionUpdated += e =>
{
    Console.WriteLine("[CloudChatSample] onSubscriptionUpdated: ", e);
};
```

イベントリスナーを活用することで、Game Chatユーザーはチャット環境の変化をリアルタイムで把握し、適切に対応することができます。


---

# 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://docs.gamechat.me/basics/game-chat-v3/game-chat-ri-ben-yu/v3nowo/ibento.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.
