-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9b69d2
commit bf410e6
Showing
3 changed files
with
88 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,11 @@ | ||
using System.Collections.Concurrent; | ||
using System.Net.WebSockets; | ||
using System.Text.Json; | ||
using System.Text; | ||
|
||
namespace HockeyPickup.Api.Helpers; | ||
|
||
public class SessionSubscriptionHandler : ISubscriptionHandler | ||
public class SessionSubscriptionHandler : BaseSubscriptionHandler | ||
{ | ||
private readonly HashSet<string> _subscribedSockets = new(); | ||
private readonly ConcurrentDictionary<string, WebSocketConnection> _connections; | ||
private readonly ConcurrentDictionary<string, string> _subscriptionIds = new(); | ||
|
||
public SessionSubscriptionHandler(ConcurrentDictionary<string, WebSocketConnection> connections) | ||
{ | ||
_connections = connections; | ||
} | ||
|
||
public string OperationType => "SessionUpdated"; | ||
|
||
public Task HandleSubscription(string socketId, string id) | ||
{ | ||
_subscriptionIds[socketId] = id; | ||
_subscribedSockets.Add(socketId); | ||
Console.WriteLine($"Added subscription with ID: {id}"); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public async Task HandleUpdate(object data) | ||
{ | ||
foreach (var socketId in _subscribedSockets) | ||
{ | ||
if (!_subscriptionIds.TryGetValue(socketId, out var subscriptionId) || | ||
!_connections.TryGetValue(socketId, out var connection) || | ||
connection.Socket.State != WebSocketState.Open) | ||
continue; | ||
|
||
try | ||
{ | ||
var message = JsonSerializer.Serialize(new | ||
{ | ||
type = "next", | ||
id = subscriptionId, // Use stored subscription ID | ||
payload = new { data = new { SessionUpdated = data } } | ||
}); | ||
public SessionSubscriptionHandler(IWebSocketService webSocketService) : base(webSocketService) { } | ||
|
||
var bytes = Encoding.UTF8.GetBytes(message); | ||
await connection.Socket.SendAsync( | ||
new ArraySegment<byte>(bytes), | ||
WebSocketMessageType.Text, | ||
true, | ||
CancellationToken.None | ||
); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error sending to socket {socketId}: {ex.Message}"); | ||
} | ||
} | ||
} | ||
public override string OperationType => "SessionUpdated"; | ||
|
||
public Task Cleanup(string socketId) | ||
{ | ||
_subscribedSockets.Remove(socketId); | ||
_subscriptionIds.TryRemove(socketId, out _); | ||
return Task.CompletedTask; | ||
} | ||
protected override object WrapData(object data) => | ||
new { data = new { SessionUpdated = data } }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters