Small refactor for receiving messages from websockets
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
38
Room/Room.cs
38
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<MessageDTO>(slicedBuffer);
|
||||
var message = JsonSerializer.Deserialize<MessageDTO>(webSocketResponse.SlicedBuffer);
|
||||
switch (message.Type)
|
||||
{
|
||||
case "GAME":
|
||||
{
|
||||
var gameInput = JsonSerializer.Deserialize<ActionDTO>(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<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()
|
||||
{
|
||||
foreach (var connection in _connections)
|
||||
@@ -106,7 +96,7 @@ public class Room
|
||||
_host = _connections.First();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public List<WebSocket> GetWebsockets()
|
||||
{
|
||||
return _connections.Select(connection => connection.Socket).ToList();
|
||||
|
||||
Reference in New Issue
Block a user