# 事件

## 事件

在 Game Chat 中，提供了处理客户端发生的各种事件的事件监听器（Event Listener）功能。通过该功能，用户可以实时监控聊天应用内发生的多种情况，并作出适当反应。以下是主要事件类型及其处理方法的说明。

## 主要事件类型

1. **消息接收**: 当接收到新的消息时触发。
2. **消息删除**: 当消息被删除时触发。
3. **错误消息**: 当发生错误时触发。
4. **连接成功**: 当成功连接到服务器时触发。
5. **连接结束**: 当服务器连接结束时触发。
6. **打字开始/结束**: 当用户开始或结束打字时分别触发。
7. **成员添加/移除**: 当用户被添加到或从频道中移除时触发。
8. **成员停用/退出**: 当用户在频道中被停用或退出时触发。

以下是如何在客户端接收事件的方法。

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

## 事件处理程序的连接与解除

使用事件处理程序可以接收各种事件，并实现所需的逻辑。 \
以下代码展示了如何连接和解除每个事件的事件处理程序。

```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);
};
```

通过利用事件监听器，游戏聊天用户可以实时了解聊天环境的变化并作出适当的响应。


---

# 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-zhong-wen/kai-shi-shi-yong-v3/shi-jian.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.
