diff --git a/RelayServer/Endpoints/RtcEndpoints.cs b/RelayServer/Endpoints/RtcEndpoints.cs index 4d06f90..ab4ebb1 100644 --- a/RelayServer/Endpoints/RtcEndpoints.cs +++ b/RelayServer/Endpoints/RtcEndpoints.cs @@ -16,6 +16,14 @@ public static class RtcEndpoints app.MapPost("/api/rtc/offer", async (RtcOffer request, RtcCallService rtcCallService) => { await rtcCallService.WriteOfferAsync(request.ChannelId, request.Username, request.SessionDescription); + + RtcNotificationService.Broadcast(new RtcNotificationMessage + { + Type = "rtc_offer_updated", + ChannelId = request.ChannelId, + Username = request.Username + }); + return Results.Ok(); }); @@ -31,14 +39,6 @@ public static class RtcEndpoints return Results.Ok(await rtcCallService.HasActiveCallAsync(channelId)); }); - // TODO: You can uncomment me if you'd like to have both. Otherwise delete me - you never know but I made it "call" instead of "active" - // Return the active call object for the specified channel. - //app.MapGet("/api/rtc/call/{channelId}", async (string channelId, RtcCallService rtcCallService) => - //{ - // var call = await rtcCallService.GetActiveCallAsync(channelId); - // return call is null ? Results.NotFound() : Results.Ok(call); - //}); - // Return the latest stored SDP offer for the specified channel. app.MapGet("/api/rtc/offers/{channelId}", async (string channelId, RtcCallService rtcCallService) => { @@ -47,9 +47,22 @@ public static class RtcEndpoints }); // Store a new SDP answer for the specified channel call. - app.MapPost("/api/rtc/answer", async (RtcOffer request, RtcCallService rtcCallService) => + app.MapPost("/api/rtc/answer", async (RtcAnswer request, RtcCallService rtcCallService) => { - await rtcCallService.WriteAnswerAsync(request.ChannelId, request.SessionDescription); + await rtcCallService.WriteAnswerAsync( + request.ChannelId, + new RtcSessionDescription + { + Type = "answer", + Sdp = request.Sdp + }); + + RtcNotificationService.Broadcast(new RtcNotificationMessage + { + Type = "rtc_answer_updated", + ChannelId = request.ChannelId + }); + return Results.Ok(); }); @@ -78,6 +91,14 @@ public static class RtcEndpoints request.Direction ); + RtcNotificationService.Broadcast(new RtcNotificationMessage + { + Type = "rtc_candidate_added", + ChannelId = request.ChannelId, + Username = request.Username, + Direction = request.Direction + }); + return Results.Ok(); }); @@ -102,6 +123,14 @@ public static class RtcEndpoints app.MapPost("/api/rtc/leave", async (RtcLeaveRequest request, RtcCallService rtcCallService) => { await rtcCallService.LeaveCallAsync(request.ChannelId, request.Username); + + RtcNotificationService.Broadcast(new RtcNotificationMessage + { + Type = "rtc_call_left", + ChannelId = request.ChannelId, + Username = request.Username + }); + return Results.Ok(); }); } diff --git a/RelayServer/Models/Rtc/RtcNotificationMessage.cs b/RelayServer/Models/Rtc/RtcNotificationMessage.cs new file mode 100644 index 0000000..89ab5e9 --- /dev/null +++ b/RelayServer/Models/Rtc/RtcNotificationMessage.cs @@ -0,0 +1,9 @@ +namespace RelayServer.Models.Rtc; + +public sealed class RtcNotificationMessage +{ + public required string Type { get; set; } // rtc_offer_updated / rtc_answer_updated / rtc_candidate_added + public required string ChannelId { get; set; } + public string? Username { get; set; } + public string? Direction { get; set; } +} \ No newline at end of file diff --git a/RelayServer/Program.cs b/RelayServer/Program.cs index 6b6f2e3..2e7ff85 100644 --- a/RelayServer/Program.cs +++ b/RelayServer/Program.cs @@ -30,8 +30,10 @@ app.MapRtcEndpoints(); var wssv = new WebSocketServer("ws://localhost:1337"); wssv.AddWebSocketService("/"); -//TODO: Create WebSocket message system which can alert clients already connected to a channel of changes to offer, answers, and ice candidates +RtcNotificationService.Server = wssv; + //TODO: Use AnswerCallback as a test on client and use the /rtc/answer endpoint call as a test on server + wssv.Start(); Console.WriteLine("WebSocket server started"); diff --git a/RelayServer/Services/Rtc/RtcNotificationService.cs b/RelayServer/Services/Rtc/RtcNotificationService.cs new file mode 100644 index 0000000..5c4f209 --- /dev/null +++ b/RelayServer/Services/Rtc/RtcNotificationService.cs @@ -0,0 +1,19 @@ +using System.Text.Json; +using RelayServer.Models.Rtc; +using WebSocketSharp.Server; + +namespace RelayServer.Services.Rtc; + +public static class RtcNotificationService +{ + public static WebSocketServer? Server { get; set; } + + public static void Broadcast(RtcNotificationMessage message) + { + if (Server is null) + return; + + var json = JsonSerializer.Serialize(message); + Server.WebSocketServices["/"]?.Sessions.Broadcast(json); + } +} \ No newline at end of file