From 64fa2e5dd88e00357e1fee804ddb3b432e6070ec Mon Sep 17 00:00:00 2001 From: AnduinXue Date: Tue, 28 Nov 2023 09:27:48 +0000 Subject: [PATCH] New connection control. --- .../Controllers/GamesController.cs | 10 +++++++++- src/Aiursoft.ChessServer/Services/WebSocketPusher.cs | 9 ++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Aiursoft.ChessServer/Controllers/GamesController.cs b/src/Aiursoft.ChessServer/Controllers/GamesController.cs index 7b1c9bf..f9ce6be 100644 --- a/src/Aiursoft.ChessServer/Controllers/GamesController.cs +++ b/src/Aiursoft.ChessServer/Controllers/GamesController.cs @@ -66,7 +66,14 @@ public class GamesController : Controller subscription = channel.Subscribe(async t => { await _pusher.SendMessage(t.Content); }); while (_pusher.Connected) { - await Task.Delay(10 * 1000); + try + { + await Task.Delay(int.MaxValue, HttpContext.RequestAborted); + } + catch (TaskCanceledException) + { + break; + } } } finally @@ -114,6 +121,7 @@ public class GamesController : Controller { return BadRequest(); } + game.Move(move); var fen = game.ToFen(); var channel = _database.GetOrAddChannel(id); diff --git a/src/Aiursoft.ChessServer/Services/WebSocketPusher.cs b/src/Aiursoft.ChessServer/Services/WebSocketPusher.cs index 8af3cdd..31ab75c 100644 --- a/src/Aiursoft.ChessServer/Services/WebSocketPusher.cs +++ b/src/Aiursoft.ChessServer/Services/WebSocketPusher.cs @@ -18,7 +18,14 @@ public class WebSocketPusher : IScopedDependency public async Task SendMessage(string message) { - await (_ws?.SendMessage(message) ?? throw new InvalidOperationException("WebSocket is not connected!")); + try + { + await (_ws?.SendMessage(message) ?? throw new InvalidOperationException("WebSocket is not connected!")); + } + catch (WebSocketException) + { + _dropped = true; + } } public async Task PendingClose()