Small refactor for receiving messages from websockets
This commit is contained in:
@@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
public class ChatOutput
|
public class ChatOutput
|
||||||
{
|
{
|
||||||
public string Playername { get; set; }
|
public string PlayerName { get; set; }
|
||||||
public string Message { 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;
|
Message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
38
Room/Room.cs
38
Room/Room.cs
@@ -27,45 +27,42 @@ public class Room
|
|||||||
var connection = AddConnection(socket);
|
var connection = AddConnection(socket);
|
||||||
if (IsEmpty()) _host = connection;
|
if (IsEmpty()) _host = connection;
|
||||||
_game.AddPlayerToGame(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();
|
BroadcastGameState();
|
||||||
var buffer = EmptyBuffer();
|
var webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||||
var result = await ReceiveAsync(webSocket, buffer);
|
while (!webSocketResponse.Result!.CloseStatus.HasValue)
|
||||||
while (!result.CloseStatus.HasValue)
|
|
||||||
{
|
{
|
||||||
var slicedBuffer = buffer[0..result.Count];
|
var message = JsonSerializer.Deserialize<MessageDTO>(webSocketResponse.SlicedBuffer);
|
||||||
var message = JsonSerializer.Deserialize<MessageDTO>(slicedBuffer);
|
|
||||||
switch (message.Type)
|
switch (message.Type)
|
||||||
{
|
{
|
||||||
case "GAME":
|
case "GAME":
|
||||||
{
|
{
|
||||||
var gameInput = JsonSerializer.Deserialize<ActionDTO>(message.Payload);
|
var gameInput = JsonSerializer.Deserialize<ActionDTO>(message.Payload);
|
||||||
_game.HandleAction(connectionId, gameInput);
|
_game.HandleAction(connection.ConnectionId, gameInput);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "CHAT":
|
case "CHAT":
|
||||||
{
|
{
|
||||||
_chat.SendChatMessage(connectionId, message.Payload);
|
_chat.SendChatMessage(connection.ConnectionId, message.Payload);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BroadcastGameState();
|
|
||||||
buffer = EmptyBuffer();
|
|
||||||
result = await ReceiveAsync(webSocket, buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription,
|
BroadcastGameState();
|
||||||
CancellationToken.None);
|
webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||||
HandleDisconnect(connectionId);
|
}
|
||||||
|
WebsocketManager.CloseAsync(connection.Socket, webSocketResponse.Result);
|
||||||
|
HandleDisconnect(connection.ConnectionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConnectionInstance AddConnection(WebSocket socket)
|
private ConnectionInstance AddConnection(WebSocket socket)
|
||||||
{
|
{
|
||||||
var connectionId = Guid.NewGuid().ToString();
|
var connectionId = Guid.NewGuid().ToString();
|
||||||
|
// TODO: Why does every connection have the same connectionId?
|
||||||
var connection = new ConnectionInstance(connectionId, socket);
|
var connection = new ConnectionInstance(connectionId, socket);
|
||||||
_connections.Add(connection);
|
_connections.Add(connection);
|
||||||
return connection;
|
return connection;
|
||||||
@@ -76,13 +73,6 @@ public class Room
|
|||||||
_connections.RemoveAll(connection => connection.ConnectionId == socketId);
|
_connections.RemoveAll(connection => connection.ConnectionId == socketId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<WebSocketReceiveResult?> ReceiveAsync(WebSocket webSocket, byte[] buffer)
|
|
||||||
{
|
|
||||||
var arraySegment = new ArraySegment<byte>(buffer);
|
|
||||||
var result = await webSocket.ReceiveAsync(arraySegment, CancellationToken.None);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void BroadcastGameState()
|
private void BroadcastGameState()
|
||||||
{
|
{
|
||||||
foreach (var connection in _connections)
|
foreach (var connection in _connections)
|
||||||
@@ -106,7 +96,7 @@ public class Room
|
|||||||
_host = _connections.First();
|
_host = _connections.First();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<WebSocket> GetWebsockets()
|
public List<WebSocket> GetWebsockets()
|
||||||
{
|
{
|
||||||
return _connections.Select(connection => connection.Socket).ToList();
|
return _connections.Select(connection => connection.Socket).ToList();
|
||||||
|
|||||||
15
Websockets/WebSocketResponse.cs
Normal file
15
Websockets/WebSocketResponse.cs
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,4 +19,18 @@ public static class WebsocketManager
|
|||||||
SendAsync(socket, message);
|
SendAsync(socket, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async Task<WebSocketResponse> ReceiveAsync(WebSocket webSocket)
|
||||||
|
{
|
||||||
|
var buffer = new byte[4096];
|
||||||
|
var arraySegment = new ArraySegment<byte>(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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user