Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

クライアント側での再接続時間オーバーしたとき、もう1回だけ再接続を試す #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion wsnet2-unity/Assets/WSNet2/Scripts/Core/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Connection

CancellationTokenSource canceller;
DateTime reconnectLimit;
Exception reconnectLimitException;

Room room;
string appId;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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)
{
Expand Down
Loading