Better messaging and better rule implementation
All checks were successful
All checks were successful
This commit is contained in:
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