diff --git a/Room/Chat/ChatOutput.cs b/Room/Chat/ChatOutput.cs index b34bef6..b7cd9c9 100644 --- a/Room/Chat/ChatOutput.cs +++ b/Room/Chat/ChatOutput.cs @@ -2,12 +2,12 @@ public class ChatOutput { - public string Playername { get; set; } + public string PlayerName { get; set; } public string Message { get; set; } - public ChatOutput(string playername, string message) + public ChatOutput(string playerName, string message) { - Playername = playername; + PlayerName = playerName; Message = message; } diff --git a/Room/Room.cs b/Room/Room.cs index cff0f4d..44aac62 100644 --- a/Room/Room.cs +++ b/Room/Room.cs @@ -27,45 +27,42 @@ public class Room var connection = AddConnection(socket); if (IsEmpty()) _host = connection; _game.AddPlayerToGame(connection); - await HandleConnection(connection.Socket, connection.ConnectionId); + await HandleConnection(connection); } - private async Task HandleConnection(WebSocket webSocket, string connectionId) + private async Task HandleConnection(ConnectionInstance connection) { BroadcastGameState(); - var buffer = EmptyBuffer(); - var result = await ReceiveAsync(webSocket, buffer); - while (!result.CloseStatus.HasValue) + var webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket); + while (!webSocketResponse.Result!.CloseStatus.HasValue) { - var slicedBuffer = buffer[0..result.Count]; - var message = JsonSerializer.Deserialize(slicedBuffer); + var message = JsonSerializer.Deserialize(webSocketResponse.SlicedBuffer); switch (message.Type) { case "GAME": { var gameInput = JsonSerializer.Deserialize(message.Payload); - _game.HandleAction(connectionId, gameInput); + _game.HandleAction(connection.ConnectionId, gameInput); break; } case "CHAT": { - _chat.SendChatMessage(connectionId, message.Payload); + _chat.SendChatMessage(connection.ConnectionId, message.Payload); break; } } - BroadcastGameState(); - buffer = EmptyBuffer(); - result = await ReceiveAsync(webSocket, buffer); - } - await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, - CancellationToken.None); - HandleDisconnect(connectionId); + BroadcastGameState(); + webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket); + } + WebsocketManager.CloseAsync(connection.Socket, webSocketResponse.Result); + HandleDisconnect(connection.ConnectionId); } private ConnectionInstance AddConnection(WebSocket socket) { var connectionId = Guid.NewGuid().ToString(); + // TODO: Why does every connection have the same connectionId? var connection = new ConnectionInstance(connectionId, socket); _connections.Add(connection); return connection; @@ -76,13 +73,6 @@ public class Room _connections.RemoveAll(connection => connection.ConnectionId == socketId); } - private static async Task ReceiveAsync(WebSocket webSocket, byte[] buffer) - { - var arraySegment = new ArraySegment(buffer); - var result = await webSocket.ReceiveAsync(arraySegment, CancellationToken.None); - return result; - } - private void BroadcastGameState() { foreach (var connection in _connections) @@ -106,7 +96,7 @@ public class Room _host = _connections.First(); } } - + public List GetWebsockets() { return _connections.Select(connection => connection.Socket).ToList(); diff --git a/Websockets/WebSocketResponse.cs b/Websockets/WebSocketResponse.cs new file mode 100644 index 0000000..a52c74a --- /dev/null +++ b/Websockets/WebSocketResponse.cs @@ -0,0 +1,15 @@ +using System.Net.WebSockets; + +namespace MauMau_Server.Websockets; + +public class WebSocketResponse +{ + public readonly WebSocketReceiveResult? Result; + public readonly byte[] SlicedBuffer; + + public WebSocketResponse(WebSocketReceiveResult? result, byte[] slicedBuffer) + { + Result = result; + SlicedBuffer = slicedBuffer; + } +} \ No newline at end of file diff --git a/Websockets/WebsocketManager.cs b/Websockets/WebsocketManager.cs index 222e32f..8941c17 100644 --- a/Websockets/WebsocketManager.cs +++ b/Websockets/WebsocketManager.cs @@ -19,4 +19,18 @@ public static class WebsocketManager SendAsync(socket, message); } } + + public static async Task ReceiveAsync(WebSocket webSocket) + { + var buffer = new byte[4096]; + var arraySegment = new ArraySegment(buffer); + var result = await webSocket.ReceiveAsync(arraySegment, CancellationToken.None); + var slicedBuffer = buffer[0..result.Count]; + return new WebSocketResponse(result, slicedBuffer); + } + + public static async void CloseAsync(WebSocket webSocket, WebSocketReceiveResult result) + { + await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); + } } \ No newline at end of file