Rename folder from Shogi.Sockets to Shogi.Api
This commit is contained in:
89
Shogi.Api/Managers/SocketConnectionManager.cs
Normal file
89
Shogi.Api/Managers/SocketConnectionManager.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using Shogi.Contracts.Socket;
|
||||
using Shogi.Api.Extensions;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Shogi.Api.Managers;
|
||||
|
||||
public interface ISocketConnectionManager
|
||||
{
|
||||
Task BroadcastToAll(ISocketResponse response);
|
||||
void Subscribe(WebSocket socket, string playerName);
|
||||
void Unsubscribe(string playerName);
|
||||
Task BroadcastToPlayers(ISocketResponse response, params string?[] playerNames);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retains all active socket connections and provides convenient methods for sending messages to clients.
|
||||
/// </summary>
|
||||
public class SocketConnectionManager : ISocketConnectionManager
|
||||
{
|
||||
/// <summary>Dictionary key is player name.</summary>
|
||||
private readonly ConcurrentDictionary<string, WebSocket> connections;
|
||||
private readonly JsonSerializerOptions serializeOptions;
|
||||
|
||||
/// <summary>Dictionary key is game name.</summary>
|
||||
private readonly ILogger<SocketConnectionManager> logger;
|
||||
|
||||
public SocketConnectionManager(ILogger<SocketConnectionManager> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.connections = new ConcurrentDictionary<string, WebSocket>();
|
||||
this.serializeOptions = new JsonSerializerOptions(JsonSerializerDefaults.General);
|
||||
|
||||
}
|
||||
|
||||
public void Subscribe(WebSocket socket, string playerName)
|
||||
{
|
||||
connections.TryRemove(playerName, out var _);
|
||||
connections.TryAdd(playerName, socket);
|
||||
}
|
||||
|
||||
public void Unsubscribe(string playerName)
|
||||
{
|
||||
connections.TryRemove(playerName, out _);
|
||||
}
|
||||
|
||||
public async Task BroadcastToPlayers(ISocketResponse response, params string?[] playerNames)
|
||||
{
|
||||
var tasks = new List<Task>(playerNames.Length);
|
||||
foreach (var name in playerNames)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(name) && connections.TryGetValue(name, out var socket))
|
||||
{
|
||||
var serialized = Serialize(response);
|
||||
logger.LogInformation("Response to {0} \n{1}\n", name, serialized);
|
||||
tasks.Add(socket.SendTextAsync(serialized));
|
||||
}
|
||||
}
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
public Task BroadcastToAll(ISocketResponse response)
|
||||
{
|
||||
var message = Serialize(response);
|
||||
logger.LogInformation("Broadcasting:\n{0}\nDone Broadcasting.", message);
|
||||
var tasks = new List<Task>(connections.Count);
|
||||
foreach (var kvp in connections)
|
||||
{
|
||||
var socket = kvp.Value;
|
||||
try
|
||||
{
|
||||
tasks.Add(socket.SendTextAsync(message));
|
||||
}
|
||||
catch (WebSocketException)
|
||||
{
|
||||
logger.LogInformation("Tried sending a message to socket connection for user [{user}], but found the connection has closed.", kvp.Key);
|
||||
Unsubscribe(kvp.Key);
|
||||
}
|
||||
catch
|
||||
{
|
||||
logger.LogInformation("Tried sending a message to socket connection for user [{user}], but found the connection has closed.", kvp.Key);
|
||||
Unsubscribe(kvp.Key);
|
||||
}
|
||||
}
|
||||
return Task.WhenAll(tasks);
|
||||
}
|
||||
|
||||
private string Serialize(object o) => JsonSerializer.Serialize(o, this.serializeOptions);
|
||||
}
|
||||
Reference in New Issue
Block a user