본문 바로가기
프로그래밍/C#_MagicOnion

[MagicOnion] Client Disconnect 사유 추가

by 뽀도 2024. 11. 7.


Client에  WaitForDisconnectAsync 추가하면 왜 연결이 끊겼는지 확인 가능한데, WaitForDisconnectAsync API는 기존 WaitForDisconnected의 개선된 버전으로, 연결이 끊어진 이유를 수신할 수있다.



🖥️ 클라이언트의 연결 함수

public async Task<bool> ConnectAsync(string address)
{
        var channel = GrpcChannelx.ForAddress($"{address}");

        _hub = await StreamingHubClient.ConnectAsync<IGamingHub, IGamingHubReceiver>(channel, this,
            serializerProvider: MemoryPackMagicOnionSerializerProvider.Instance);

        if (_hub is null)
        {
            Debug.Log("Failed to connect to Hub");
            return false;
        }
        
        // Disconnect Event 처리 
        _ = WaitForDisconnectEventAsync();
        
        async Task WaitForDisconnectEventAsync()
        {
            var reason = await _hub.WaitForDisconnectAsync();
            Debug.Log("WaitForDisconnectEventAsync: " + reason.Type);
        }
        
        return true;
}


👍 연결이 끊어진 이유

public enum DisconnectionType
{
    /// <summary>
    /// Disconnected after completing successfully.
    /// </summary>
    CompletedNormally = 0,

    /// <summary>
    /// Disconnected due to exception while reading messages.
    /// </summary>
    Faulted = 1,

    /// <summary>
    /// Disconnected due to reaching the heartbeat timeout.
    /// </summary>
    TimedOut = 2,
}


서버를 강제로 종료하거나 클라이언트의 인터넷이 끊겼을때 DisconnectionType.Fauled로 처리하는 것을 확인 하였다. 
DisconnectionType에 따라 예외처리를 진행하면 될 듯 하다.



[참고] 원본

https://github.com/Cysharp/MagicOnion/pull/837

 

Introduce WaitForDisconnectAsync by mayuki · Pull Request #837 · Cysharp/MagicOnion

This PR introduces a new API WaitForDisconnectAsync to StreamingHubClient. WaitForDisconnectAsync API is an updated version of the existing WaitForDisconnected, and it will be possible to receive t...

github.com

 

반응형

댓글