# 이벤트

## 이벤트

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/v3/undefined-4.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.
