Added room functionality
This commit is contained in:
74
Websockets/Room.cs
Normal file
74
Websockets/Room.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
|
||||
namespace MauMau_Server.Websockets;
|
||||
|
||||
public class Room
|
||||
{
|
||||
private readonly Dictionary<string, WebSocket> _connections = new();
|
||||
|
||||
public async Task InstantiateConnection(WebSocket socket)
|
||||
{
|
||||
var socketId = AddConnection(socket);
|
||||
await HandleConnection(socket, socketId);
|
||||
}
|
||||
|
||||
private async Task HandleConnection(WebSocket socket, string socketId)
|
||||
{
|
||||
var buffer = EmptyBuffer();
|
||||
var result = await ReceiveAsync(socket, buffer);
|
||||
while (!result.CloseStatus.HasValue)
|
||||
{
|
||||
var message = $"{socketId}: {Encoding.Default.GetString(buffer)}";
|
||||
BroadcastAsync(message);
|
||||
buffer = EmptyBuffer();
|
||||
result = await ReceiveAsync(socket, buffer);
|
||||
}
|
||||
await socket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
|
||||
RemoveConnection(socketId);
|
||||
}
|
||||
|
||||
private string AddConnection(WebSocket socket)
|
||||
{
|
||||
var socketId = Guid.NewGuid().ToString();
|
||||
_connections.Add(socketId, socket);
|
||||
return socketId;
|
||||
}
|
||||
|
||||
public WebSocket GetConnection(string socketId)
|
||||
{
|
||||
return _connections[socketId];
|
||||
}
|
||||
|
||||
public Dictionary<string, WebSocket> GetAllConnections()
|
||||
{
|
||||
return _connections;
|
||||
}
|
||||
|
||||
public void RemoveConnection(string socketId)
|
||||
{
|
||||
_connections.Remove(socketId);
|
||||
}
|
||||
|
||||
private 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 BroadcastAsync(string message)
|
||||
{
|
||||
var bytes = Encoding.Default.GetBytes(message);
|
||||
var arraySegment = new ArraySegment<byte>(bytes);
|
||||
foreach (var (id, socket) in GetAllConnections())
|
||||
{
|
||||
socket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] EmptyBuffer()
|
||||
{
|
||||
return new byte[4096];
|
||||
}
|
||||
}
|
||||
43
Websockets/RoomManager.cs
Normal file
43
Websockets/RoomManager.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
namespace MauMau_Server.Websockets;
|
||||
|
||||
public class RoomManager : IRoomManager
|
||||
{
|
||||
private static readonly Dictionary<string, Room> Rooms = new();
|
||||
|
||||
public string CreateRoom()
|
||||
{
|
||||
var roomId = Guid.NewGuid().ToString();
|
||||
var room = new Room();
|
||||
Rooms.Add(roomId, room);
|
||||
return roomId;
|
||||
}
|
||||
|
||||
public Room GetRoom(string roomId)
|
||||
{
|
||||
return Rooms[roomId];
|
||||
}
|
||||
|
||||
public List<string> GetAllRooms()
|
||||
{
|
||||
return Rooms.Keys.ToList();
|
||||
}
|
||||
|
||||
public void RemoveRoom(string roomId)
|
||||
{
|
||||
Rooms.Remove(roomId);
|
||||
}
|
||||
|
||||
public bool RoomExists(string roomId)
|
||||
{
|
||||
return Rooms.ContainsKey(roomId);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IRoomManager
|
||||
{
|
||||
public string CreateRoom();
|
||||
public Room GetRoom(string roomId);
|
||||
public List<string> GetAllRooms();
|
||||
public void RemoveRoom(string roomId);
|
||||
public bool RoomExists(string roomId);
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace MauMau_Server.Websockets;
|
||||
|
||||
public class WebsocketManager : IWebsocketManager
|
||||
{
|
||||
private static readonly Dictionary<string, WebSocket> Connections = new();
|
||||
|
||||
public string AddConnection(WebSocket socket)
|
||||
{
|
||||
var id = Guid.NewGuid().ToString();
|
||||
Connections.Add(id, socket);
|
||||
return id;
|
||||
}
|
||||
|
||||
public WebSocket GetConnection(string id)
|
||||
{
|
||||
return Connections[id];
|
||||
}
|
||||
|
||||
public Dictionary<string, WebSocket> GetAllConnections()
|
||||
{
|
||||
return Connections;
|
||||
}
|
||||
|
||||
public void RemoveConnection(string id)
|
||||
{
|
||||
Connections.Remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IWebsocketManager
|
||||
{
|
||||
public string AddConnection(WebSocket socket);
|
||||
public WebSocket GetConnection(string id);
|
||||
public Dictionary<string, WebSocket> GetAllConnections();
|
||||
public void RemoveConnection(string id);
|
||||
}
|
||||
Reference in New Issue
Block a user