Better messaging and better rule implementation
All checks were successful
All checks were successful
This commit is contained in:
@@ -21,13 +21,13 @@ public class Lobby : RoomType
|
||||
|
||||
public override void OnConnect(ConnectionInstance connection)
|
||||
{
|
||||
var joinMessage = new JoinMessage(connection.Id, connection.Name + " has joined the room.");
|
||||
var joinMessage = new JoinMessage(_room.Connections, connection);
|
||||
_room.BroadCast(new RoomMessage<JoinMessage>("JOIN", joinMessage));
|
||||
}
|
||||
|
||||
public override void OnDisconnect(ConnectionInstance connection)
|
||||
{
|
||||
var leaveMessage = new LeaveMessage(connection.Id, connection.Name + " has left the room.");
|
||||
var leaveMessage = new LeaveMessage(connection);
|
||||
_room.BroadCast(new RoomMessage<LeaveMessage>("LEAVE", leaveMessage));
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,15 @@
|
||||
namespace MauMau_Server.Room.Messages;
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
namespace MauMau_Server.Room.Messages;
|
||||
|
||||
public class JoinMessage
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string ChatMessage { get; set; }
|
||||
public string? Data { get; set; }
|
||||
public List<ConnectionInstance> Connections { get; set; }
|
||||
public ConnectionInstance NewConnection { get; set; }
|
||||
|
||||
public JoinMessage(Guid id, string chatMessage, string data)
|
||||
public JoinMessage(List<ConnectionInstance> connections, ConnectionInstance newConnection)
|
||||
{
|
||||
Id = id;
|
||||
ChatMessage = chatMessage;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public JoinMessage(Guid id, string chatMessage)
|
||||
{
|
||||
Id = id;
|
||||
ChatMessage = chatMessage;
|
||||
Connections = connections;
|
||||
NewConnection = newConnection;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
namespace MauMau_Server.Room.Messages;
|
||||
using MauMau_Server.Websockets;
|
||||
|
||||
namespace MauMau_Server.Room.Messages;
|
||||
|
||||
public class LeaveMessage
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string ChatMessage { get; set; }
|
||||
public ConnectionInstance Connection { get; set; }
|
||||
|
||||
public LeaveMessage(Guid id, string chatMessage)
|
||||
public LeaveMessage(ConnectionInstance connection)
|
||||
{
|
||||
Id = id;
|
||||
ChatMessage = chatMessage;
|
||||
Connection = connection;
|
||||
}
|
||||
}
|
||||
66
Room/Room.cs
66
Room/Room.cs
@@ -3,6 +3,7 @@ using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using MauMau_Server.Room.Messages;
|
||||
using MauMau_Server.Websockets;
|
||||
using Microsoft.AspNet.SignalR.Messaging;
|
||||
|
||||
namespace MauMau_Server.Room;
|
||||
|
||||
@@ -24,10 +25,16 @@ public class Room
|
||||
public async Task InstantiateConnection(WebSocket socket, string name)
|
||||
{
|
||||
var connectionId = Guid.NewGuid();
|
||||
var connection = new ConnectionInstance(name, connectionId, socket);
|
||||
|
||||
// If the name is empty, set it to "Mau" + the first part of the connection ID, otherwise strip potential HTML from the name and use it
|
||||
var validatedName = !string.IsNullOrWhiteSpace(name)
|
||||
? StripHTML(name)
|
||||
: "Mau" + connectionId.ToString().Split('-')[0];
|
||||
|
||||
var connection = new ConnectionInstance(validatedName, connectionId, socket);
|
||||
if (IsEmpty()) Host = connection;
|
||||
Connections.Add(connection);
|
||||
|
||||
|
||||
RoomType.OnConnect(connection);
|
||||
await HandleConnection(connection);
|
||||
}
|
||||
@@ -38,27 +45,60 @@ public class Room
|
||||
while (!webSocketResponse.Result!.CloseStatus.HasValue)
|
||||
{
|
||||
var message = JsonSerializer.Deserialize<RoomMessage<string>>(webSocketResponse.SlicedBuffer);
|
||||
if (message.Type == "CHAT")
|
||||
switch (message.Type)
|
||||
{
|
||||
var cleanedMessage = StripHTML(message.Data);
|
||||
if (string.IsNullOrWhiteSpace(cleanedMessage)) cleanedMessage = "Mau!";
|
||||
var chatMessage = new ChatMessage(connection.Name, cleanedMessage);
|
||||
BroadCast(new RoomMessage<ChatMessage>("CHAT", chatMessage));
|
||||
}
|
||||
else
|
||||
{
|
||||
RoomType.OnMessage(connection, message);
|
||||
case "CHAT":
|
||||
HandleChatMessage(connection, message.Data);
|
||||
break;
|
||||
case "KICK":
|
||||
await HandleKick(Guid.Parse(message.Data));
|
||||
break;
|
||||
default:
|
||||
RoomType.OnMessage(connection, message);
|
||||
break;
|
||||
}
|
||||
|
||||
webSocketResponse = await WebsocketManager.ReceiveAsync(connection.Socket);
|
||||
}
|
||||
|
||||
WebsocketManager.CloseAsync(connection.Socket, webSocketResponse.Result);
|
||||
HandleDisconnect(connection);
|
||||
}
|
||||
|
||||
private void HandleChatMessage(ConnectionInstance sender, string chatMessage)
|
||||
{
|
||||
// Remove HTML from chat message to prevent HTML injection
|
||||
var cleanedMessage = StripHTML(chatMessage);
|
||||
|
||||
// If the message is empty, set it to "Mau!"
|
||||
if (string.IsNullOrWhiteSpace(cleanedMessage)) cleanedMessage = "Mau!";
|
||||
|
||||
// Create a new chat message object with the sender and the message
|
||||
var envelope = new ChatMessage(sender.Name, cleanedMessage);
|
||||
|
||||
// Broadcast the chat message to all connections in the room
|
||||
BroadCast(new RoomMessage<ChatMessage>("CHAT", envelope));
|
||||
}
|
||||
|
||||
private async Task HandleKick(Guid connectionId)
|
||||
{
|
||||
// Search for the connection with the given ID
|
||||
var connection = Connections.FirstOrDefault(connection => connection.Id == connectionId);
|
||||
|
||||
// If the connection could not be found, return
|
||||
if (connection == null) return;
|
||||
|
||||
// Handle the disconnect of the connection
|
||||
HandleDisconnect(connection);
|
||||
|
||||
// Close the connection with the reason "You have been kicked"
|
||||
await connection.Socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "You have been kicked",
|
||||
CancellationToken.None);
|
||||
}
|
||||
|
||||
private void HandleDisconnect(ConnectionInstance connection)
|
||||
{
|
||||
Connections.Remove(connection);
|
||||
Connections.Remove(connection);
|
||||
RoomType.OnDisconnect(connection);
|
||||
if (IsEmpty())
|
||||
{
|
||||
@@ -84,7 +124,7 @@ public class Room
|
||||
{
|
||||
return Connections.Count == 0;
|
||||
}
|
||||
|
||||
|
||||
private static string StripHTML(string input)
|
||||
{
|
||||
return Regex.Replace(input, "<.*?>", string.Empty);
|
||||
|
||||
Reference in New Issue
Block a user