diff --git a/wsnet2-unity/Assets/WSNet2/Scripts/Core/Connection.cs b/wsnet2-unity/Assets/WSNet2/Scripts/Core/Connection.cs index 7f0a0ce..aff918b 100644 --- a/wsnet2-unity/Assets/WSNet2/Scripts/Core/Connection.cs +++ b/wsnet2-unity/Assets/WSNet2/Scripts/Core/Connection.cs @@ -26,6 +26,7 @@ class Connection CancellationTokenSource canceller; DateTime reconnectLimit; + Exception reconnectLimitException; Room room; string appId; @@ -56,6 +57,7 @@ public Connection(Room room, string clientId, HMAC hmac, JoinedRoom joined, Logg this.logger = logger; this.canceller = new CancellationTokenSource(); this.reconnectLimit = DateTime.Now.AddSeconds(room.ClientDeadline); + this.reconnectLimitException = null; this.room = room; this.appId = joined.roomInfo.appId; this.clientId = clientId; @@ -168,9 +170,16 @@ await await Task.WhenAny( } catch { } + if (reconnectLimitException != null) + { + // 再接続時間切れの次の試行で失敗したら終わり + throw new AggregateException($"Gave up on Reconnection", reconnectLimitException, lastException); + } + if (DateTime.Now > reconnectLimit) { - throw new Exception($"Gave up on Reconnection: {lastException.Message}", lastException); + // 再接続時間切れしてももう一回試す + reconnectLimitException = lastException; } reconnection++; @@ -239,6 +248,7 @@ private async Task Receiver(ClientWebSocket ws, CancellationToken ct) var ev = await ReceiveEvent(ws, ct); this.reconnectLimit = DateTime.Now.AddSeconds(room.ClientDeadline); + this.reconnectLimitException = null; if (ev.IsRegular) {