Update: Needs Testing - Not Tested

This commit is contained in:
2026-04-04 16:17:57 -04:00
parent c89a0cf88b
commit 3f27c94032
4 changed files with 70 additions and 11 deletions

View File

@@ -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();
});
}

View File

@@ -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; }
}

View File

@@ -30,8 +30,10 @@ app.MapRtcEndpoints();
var wssv = new WebSocketServer("ws://localhost:1337");
wssv.AddWebSocketService<ChatSocketBehavior>("/");
//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");

View File

@@ -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);
}
}