♻️ Replace the LiveKit with built-in webrtc

This commit is contained in:
2025-10-19 17:30:51 +08:00
parent 46ebd92dc1
commit 4626529eb5
6 changed files with 925 additions and 127 deletions

View File

@@ -74,4 +74,4 @@ public class WebSocketPacket
ErrorMessage = packet.ErrorMessage
};
}
}
}

View File

@@ -15,7 +15,6 @@ public partial class ChatService(
FileService.FileServiceClient filesClient,
FileReferenceService.FileReferenceServiceClient fileRefs,
IServiceScopeFactory scopeFactory,
IRealtimeService realtime,
ILogger<ChatService> logger
)
{
@@ -535,27 +534,10 @@ public partial class ChatService(
{
RoomId = room.Id,
SenderId = sender.Id,
ProviderName = realtime.ProviderName
ProviderName = "Built-in WebRTC Signaling",
SessionId = Guid.NewGuid().ToString() // Simple session ID for built-in signaling
};
try
{
var sessionConfig = await realtime.CreateSessionAsync(room.Id, new Dictionary<string, object>
{
{ "room_id", room.Id },
{ "user_id", sender.AccountId },
});
// Store session details
call.SessionId = sessionConfig.SessionId;
call.UpstreamConfig = sessionConfig.Parameters;
}
catch (Exception ex)
{
// Log the exception but continue with call creation
throw new InvalidOperationException($"Failed to create {realtime.ProviderName} session: {ex.Message}");
}
db.ChatRealtimeCall.Add(call);
await db.SaveChangesAsync();
@@ -580,26 +562,7 @@ public partial class ChatService(
if (sender.Role < ChatMemberRole.Moderator && call.SenderId != sender.Id)
throw new InvalidOperationException("You are not the call initiator either the chat room moderator.");
// End the realtime session if it exists
if (!string.IsNullOrEmpty(call.SessionId) && !string.IsNullOrEmpty(call.ProviderName))
{
try
{
var config = new RealtimeSessionConfig
{
SessionId = call.SessionId,
Parameters = call.UpstreamConfig
};
await realtime.EndSessionAsync(call.SessionId, config);
}
catch (Exception ex)
{
// Log the exception but continue with call ending
throw new InvalidOperationException($"Failed to end {call.ProviderName} session: {ex.Message}");
}
}
// For built-in WebRTC signaling, just set the end time
call.EndedAt = SystemClock.Instance.GetCurrentInstant();
db.ChatRealtimeCall.Update(call);
await db.SaveChangesAsync();

View File

@@ -0,0 +1,635 @@
# WebRTC Signaling Server - Client Implementation Guide
This document explains how clients should implement WebRTC signaling to work with the DysonNetwork WebRTC server.
## Overview
The WebRTC signaling server provides a WebSocket-based signaling channel for WebRTC peer-to-peer communication within chat rooms. It handles authentication, room membership verification, and message broadcasting between clients in the same chat room.
When using with the Gateway, the `/api` should be replaced with `<gateway>/sphere`
## Architecture
- **Signaling Endpoint**: `GET /api/chat/realtime/{chatId}`
- **Authentication**: JWT-based (handled by existing middleware)
- **Message Format**: WebSocketPacket (structured JSON packets)
- **Protocol**: Room-based broadcasting with client management and enforced sender validation
## Client Implementation
### 1. Prerequisites
Before implementing WebRTC signaling, ensure your client:
1. **Has Valid Authentication**: Must provide a valid JWT token for the authenticated user
2. **Is a Chat Room Member**: User must be an active member of the specified chat room
3. **Supports WebSockets**: Must be capable of establishing WebSocket connections
### 2. Connection Establishment
#### 2.1 WebSocket Connection URL
```
ws://your-server.com/api/chat/realtime/{chatId}
```
- **Protocol**: `ws://` (or `wss://` for secure connections)
- **Path**: `/api/chat/realtime/{chatId}` where `{chatId}` is the chat room GUID
- **Authentication**: Handled via existing JWT middleware (no additional query parameters needed)
#### 2.2 Authentication
The authentication is handled automatically by the server's middleware that:
1. Checks for valid JWT token in the request
2. Extracts the authenticated user (`Account`) from `HttpContext.Items["CurrentUser"]`
3. Validates that the user is a member of the specified chat room
4. Returns `401 Unauthorized` if not authenticated or `403 Forbidden` if not a room member
#### 2.3 Connection Example (JavaScript)
```javascript
class SignalingClient {
constructor(chatId, serverUrl = 'ws://localhost:5000', userId, userName) {
this.chatId = chatId;
this.ws = null;
this.serverUrl = serverUrl;
this.isConnected = false;
this.userId = userId; // Current user ID
this.userName = userName; // Current user name
this.onMessageHandlers = [];
}
// Connect to the signaling server
async connect() {
const url = `${this.serverUrl}/api/chat/realtime/${this.chatId}`;
try {
this.ws = new WebSocket(url);
this.ws.onopen = (event) => {
this.isConnected = true;
console.log('Connected to signaling server for chat:', this.chatId);
};
this.ws.onmessage = (event) => {
this.handleMessage(event.data);
};
this.ws.onclose = (event) => {
this.isConnected = false;
console.log('Disconnected from signaling server');
};
this.ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
} catch (error) {
console.error('Failed to connect to signaling server:', error);
throw error;
}
}
// Disconnect from the signaling server
disconnect() {
if (this.ws && this.isConnected) {
this.ws.close();
this.isConnected = false;
}
}
}
```
### 3. Message Handling
#### 3.1 Enforced Message Format
The signaling server broadcasts messages using the WebSocketPacket format. All messages are automatically wrapped by the server with validated sender information. Clients should send only the signaling type and data, and receive complete packets with sender details.
**WebSocketPacket Format:**
For signaling messages:
```json
{
"type": "signaling",
"data": {
"type": "signaling-message-type",
"data": {
"offer": "...SDP string here...",
"answer": "...SDP string here...",
"candidate": {...ICE candidate data...}
},
"senderAccountId": "server-validated-user-guid",
"senderInfo": {
// Full SnAccount model with user details
"id": "user-guid",
"name": "username",
"nick": "display nickname",
"profile": { ... },
// ... complete account information
}
}
}
```
For connection established:
```json
{
"type": "webrtc",
"data": {
// welcome data
}
}
```
#### 3.2 Incoming Messages
Implement a message handler to process signaling data with user identity:
```javascript
class SignalingClient {
constructor(chatId, serverUrl = 'ws://localhost:5000', userId, userName) {
this.chatId = chatId;
this.ws = null;
this.serverUrl = serverUrl;
this.isConnected = false;
this.userId = userId; // Current user ID
this.userName = userName; // Current user name
this.onMessageHandlers = [];
}
// ... WebSocket connection methods ...
handleMessage(message) {
try {
// Parse WebSocketPacket
const packet = JSON.parse(message);
if (packet.type === 'signaling') {
// Extract signaling message with server-validated sender info
const signalingMessage = packet.data;
const senderId = signalingMessage.SenderAccountId;
const senderInfo = signalingMessage.SenderInfo;
// Ignore messages from yourself (server broadcasts to all clients)
if (senderId === this.userId) {
return;
}
// Use sender's nick or name for display
const senderDisplay = senderInfo?.nick || senderInfo?.name || senderId;
console.log(`Received ${signalingMessage.type} from ${senderDisplay} (${senderId})`);
// Call handlers with signal type and data and sender info
this.onMessageHandlers.forEach(handler => {
try {
handler(signalingMessage, senderId, senderInfo);
} catch (error) {
console.error('Error in message handler:', error);
}
});
} else if (packet.type === 'webrtc') {
// Handle connection established or other server messages
console.log('Received server message:', packet.data.message);
} else {
console.warn('Unknown packet type:', packet.type);
}
} catch (error) {
console.error('Failed to parse WebSocketPacket:', message, error);
}
}
// Register message handlers
onMessage(handler) {
this.onMessageHandlers.push(handler);
return () => {
// Return unsubscribe function
const index = this.onMessageHandlers.indexOf(handler);
if (index > -1) {
this.onMessageHandlers.splice(index, 1);
}
};
}
sendMessage(messageData) {
if (!this.isConnected || !this.ws || this.ws.readyState !== WebSocket.OPEN) {
console.warn('Cannot send message: WebSocket not connected');
return false;
}
try {
// Server will automatically add sender info - just send the signaling data
const messageStr = JSON.stringify(messageData);
this.ws.send(messageStr);
return true;
} catch (error) {
console.error('Failed to send message:', error);
return false;
}
}
}
```
#### 3.3 User Identity Tracking
Track connected peers with full account information:
```javascript
class SignalingClient {
constructor(chatId, serverUrl, userId, userName) {
this.chatId = chatId;
this.userId = userId;
this.userName = userName;
this.serverUrl = serverUrl;
this.ws = null;
this.isConnected = false;
this.connectedPeers = new Map(); // userId -> senderInfo
this.onPeerHandlers = [];
this.onMessageHandlers = [];
}
handleMessage(message) {
try {
const packet = JSON.parse(message);
if (packet.type === 'signaling') {
const signalingMessage = packet.data;
const senderId = signalingMessage.SenderAccountId;
const senderInfo = signalingMessage.SenderInfo;
// Track peer information with full account data
if (!this.connectedPeers.has(senderId)) {
this.connectedPeers.set(senderId, senderInfo);
this.onPeerHandlers.forEach(handler => {
try {
handler(senderId, senderInfo, 'connected');
} catch (error) {
console.error('Error in peer handler:', error);
}
});
console.log(`New peer connected: ${senderInfo?.name || senderId} (${senderId})`);
}
// Ignore messages from yourself
if (senderId === this.userId) {
return;
}
// Call handlers with signaling message and sender info
this.onMessageHandlers.forEach(handler => {
try {
handler(signalingMessage, senderId, senderInfo);
} catch (error) {
console.error('Error in message handler:', error);
}
});
} else if (packet.type === 'webrtc') {
// Handle connection established or other server messages
console.log('Received server message:', packet.data.message);
} else {
console.warn('Unknown packet type:', packet.type);
}
} catch (error) {
console.error('Failed to parse WebSocketPacket:', message, error);
}
}
// Register peer connection/disconnection handlers
onPeer(handler) {
this.onPeerHandlers.push(handler);
return () => {
const index = this.onPeerHandlers.indexOf(handler);
if (index > -1) {
this.onPeerHandlers.splice(index, 1);
}
};
}
// Get list of connected peers with full account info
getConnectedPeers() {
return Array.from(this.connectedPeers.entries()).map(([userId, senderInfo]) => ({
userId,
userInfo: senderInfo
}));
}
// Find user info by user ID
getUserInfo(userId) {
return this.connectedPeers.get(userId);
}
}
```
### 4. WebRTC Integration
#### 4.1 Complete Implementation Example
```javascript
class WebRTCCPUB extends SignalingClient {
constructor(chatId, serverUrl) {
super(chatId, serverUrl);
this.peerConnection = null;
this.localStream = null;
this.remoteStream = null;
// Initialize WebRTCPeerConnection with configuration
this.initPeerConnection();
}
initPeerConnection() {
const configuration = {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:stun1.l.google.com:19302' }
]
};
this.peerConnection = new RTCPeerConnection(configuration);
// Handle ICE candidates
this.peerConnection.onicecandidate = (event) => {
if (event.candidate) {
// Send ICE candidate via signaling server
this.sendMessage({
type: 'ice-candidate',
candidate: event.candidate
});
}
};
// Handle remote stream
this.peerConnection.ontrack = (event) => {
this.remoteStream = event.streams[0];
// Attach remote stream to video element
if (this.onRemoteStream) {
this.onRemoteStream(this.remoteStream);
}
};
}
// Register for signaling messages
onMessage(signalingMessage, senderId, senderInfo) {
super.onMessage(signalingMessage, senderId, senderInfo).then(() => {
this.handleSignalingMessage(signalingMessage);
});
}
handleSignalingMessage(signalingMessage) {
switch (signalingMessage.type) {
case 'offer':
this.handleOffer(signalingMessage.data.offer);
break;
case 'answer':
this.handleAnswer(signalingMessage.data.answer);
break;
case 'ice-candidate':
this.handleIceCandidate(signalingMessage.data.candidate);
break;
default:
console.warn('Unknown message type:', signalingMessage.type);
}
}
async createOffer() {
try {
const offer = await this.peerConnection.createOffer();
await this.peerConnection.setLocalDescription(offer);
// Send offer via signaling server
this.sendMessage({
type: 'offer',
offer: offer
});
} catch (error) {
console.error('Error creating offer:', error);
}
}
async handleOffer(offer) {
try {
await this.peerConnection.setRemoteDescription(new RTCSessionDescription(offer));
const answer = await this.peerConnection.createAnswer();
await this.peerConnection.setLocalDescription(answer);
// Send answer via signaling server
this.sendMessage({
type: 'answer',
answer: answer
});
} catch (error) {
console.error('Error handling offer:', error);
}
}
async handleAnswer(answer) {
try {
await this.peerConnection.setRemoteDescription(new RTCSessionDescription(answer));
} catch (error) {
console.error('Error handling answer:', error);
}
}
async handleIceCandidate(candidate) {
try {
await this.peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
} catch (error) {
console.error('Error handling ICE candidate:', error);
}
}
// Get user media and add to peer connection
async startLocalStream(constraints = { audio: true, video: true }) {
try {
this.localStream = await navigator.mediaDevices.getUserMedia(constraints);
this.localStream.getTracks().forEach(track => {
this.peerConnection.addTrack(track, this.localStream);
});
return this.localStream;
} catch (error) {
console.error('Error accessing media devices:', error);
throw error;
}
}
}
```
### 5. Usage Flow
#### 5.1 Basic Usage Pattern
```javascript
// 1. Create signaling client
const signaling = new WebRTCCPUB(chatId, serverUrl);
// 2. Set up event handlers
signaling.onRemoteStream = (stream) => {
// Attach remote stream to video element
remoteVideoElement.srcObject = stream;
};
// 3. Connect to signaling server
await signaling.connect();
// 4. Get local media stream
await signaling.startLocalStream();
// 5. Create offer (for the caller)
await signaling.createOffer();
// The signaling server will automatically broadcast messages to other clients in the room
```
#### 5.2 Complete Call Flow Example
```javascript
async function initiateCall(chatId, serverUrl) {
const caller = new WebRTCCPUB(chatId, serverUrl);
// Connect to signaling server
await caller.connect();
// Get local stream
const localStream = await caller.startLocalStream();
localVideoElement.srcObject = localStream;
// Create and send offer
await caller.createOffer();
// Wait for remote stream
caller.onRemoteStream = (remoteStream) => {
remoteVideoElement.srcObject = remoteStream;
console.log('Call connected!');
};
}
async function answerCall(chatId, serverUrl) {
const answerer = new WebRTCCPUB(chatId, serverUrl);
// Connect to signaling server
await answerer.connect();
// Get local stream
const localStream = await answerer.startLocalStream();
localVideoElement.srcObject = localStream;
// WebRTC signaling is handled automatically by the message handlers
answerer.onRemoteStream = (remoteStream) => {
remoteVideoElement.srcObject = remoteStream;
console.log('Call connected!');
};
}
```
### 6. Error Handling
#### 6.1 Connection Errors
```javascript
// Handle connection errors
signaling.ws.addEventListener('error', (event) => {
console.error('WebSocket connection error:', event);
// Attempt reconnection or show error to user
});
// Handle server close
signaling.ws.addEventListener('close', (event) => {
console.log('WebSocket closed:', event.code, event.reason);
// Reconnect if clean closure
if (event.wasClean) {
// Re-establish connection if needed
} else {
// Report error
}
});
```
#### 6.2 WebRTC Errors
```javascript
// Handle getUserMedia errors
try {
const stream = await navigator.mediaDevices.getUserMedia(constraints);
} catch (error) {
switch (error.name) {
case 'NotAllowedError':
console.error('User denied media access');
break;
case 'NotFoundError':
console.error('No media devices found');
break;
default:
console.error('Error accessing media:', error);
}
}
```
### 7. Best Practices
#### 7.1 Connection Management
- **Reconnection Logic**: Implement exponential backoff for reconnection attempts
- **Connection Pooling**: Re-use connections when possible
- **Cleanup**: Always close connections and clean up resources
#### 7.2 Message Handling
- **Message Validation**: Validate incoming signaling messages
- **Error Resilience**: Gracefully handle malformed messages
- **Message Types**: Define clear message type conventions
#### 7.3 WebRTC Configuration
- **ICE Servers**: Configure multiple STUN/TURN servers for reliability
- **Codec Preferences**: Set preferred codecs for optimal performance
- **Bandwidth Management**: Implement appropriate bitrate controls
#### 7.4 Security Considerations
- **Input Validation**: Validate all signaling data
- **Rate Limiting**: Implement appropriate rate limiting for signaling messages
- **Authentication**: Ensure proper authentication before establishing connections
### 8. Room Isolation
The signaling server guarantees that:
- **Messages stay within rooms**: Clients only receive messages from other clients in the same chat room
- **Authentication per connection**: Each WebSocket connection is individually authenticated
- **Member validation**: Only active chat room members can connect and send messages
### 9. Troubleshooting
#### 9.1 Common Issues
- **Connection refused**: Check if JWT token is valid and user is room member
- **Messages not received**: Verify room membership and connection status
- **WebRTC failures**: Check ICE server configuration and network connectivity
#### 9.2 Debug Tips
- Enable console logging for signaling events
- Monitor WebSocket connection state
- Validate signaling message formats
- Check browser developer tools for network activity
## API Reference
### WebSocket Endpoint
- **URL Pattern**: `/api/chat/realtime/{chatId}`
- **Method**: `GET`
- **Authentication**: JWT (middleware-handled)
- **Protocol**: WebSocket (ws/wss)
### Response Codes
- **401**: Unauthorized - Invalid or missing JWT
- **403**: Forbidden - User not member of chat room
- **400**: Bad Request - Not a WebSocket request
### Message Format
- **Encoding**: UTF-8 text
- **Format**: WebSocketPacket JSON (server-enforced structure)
- **Broadcasting**: Automatic to all room members except sender with validated sender information
## Additional Resources
- [WebRTC API Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API)
- [WebSocket API Documentation](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
- [WebRTC Signaling Fundamentals](https://webrtc.org/getting-started/signaling-channels)

View File

@@ -1,10 +1,12 @@
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Sphere.Chat.Realtime;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Swashbuckle.AspNetCore.Annotations;
using System.Collections.Concurrent;
using System.Net.WebSockets;
using DysonNetwork.Shared.Proto;
using WebSocketPacket = DysonNetwork.Shared.Models.WebSocketPacket;
namespace DysonNetwork.Sphere.Chat;
@@ -13,6 +15,14 @@ public class RealtimeChatConfiguration
public string Endpoint { get; set; } = null!;
}
public class SignalingMessage
{
public string Type { get; set; } = null!;
public object? Data { get; set; }
public string? AccountId { get; set; }
public SnAccount? Account { get; set; }
}
[ApiController]
[Route("/api/chat/realtime")]
public class RealtimeCallController(
@@ -20,31 +30,36 @@ public class RealtimeCallController(
AppDatabase db,
ChatService cs,
ChatRoomService crs,
IRealtimeService realtime
ILogger<RealtimeCallController> logger
) : ControllerBase
{
private readonly RealtimeChatConfiguration _config =
configuration.GetSection("RealtimeChat").Get<RealtimeChatConfiguration>()!;
// A thread-safe collection to hold connected WebSocket clients per chat room.
private static readonly
ConcurrentDictionary<string, ConcurrentDictionary<Guid, (WebSocket Socket, string
AccountId, int Role)>> RoomClients = new();
// A thread-safe collection to hold participants in each room.
private static readonly
ConcurrentDictionary<string, ConcurrentDictionary<string, (Account Account, DateTime JoinedAt)>>
RoomParticipants = new();
/// <summary>
/// This endpoint is especially designed for livekit webhooks,
/// for update the call participates and more.
/// Learn more at: https://docs.livekit.io/home/server/webhooks/
/// This endpoint is for WebRTC signaling webhooks if needed in the future.
/// Currently built-in WebRTC signaling doesn't require external webhooks.
/// </summary>
[HttpPost("webhook")]
[SwaggerIgnore]
public async Task<IActionResult> WebhookReceiver()
public Task<IActionResult> WebhookReceiver()
{
using var reader = new StreamReader(Request.Body);
var postData = await reader.ReadToEndAsync();
var authHeader = Request.Headers.Authorization.ToString();
await realtime.ReceiveWebhook(postData, authHeader);
return Ok();
// Built-in WebRTC signaling doesn't require webhooks
// Return success to indicate endpoint exists for potential future use
return Task.FromResult<IActionResult>(Ok("Webhook received - built-in WebRTC signaling active"));
}
[HttpGet("{roomId:guid}")]
[HttpGet("{roomId:guid}/status")]
[Authorize]
public async Task<ActionResult<SnRealtimeCall>> GetOngoingCall(Guid roomId)
{
@@ -94,46 +109,32 @@ public class RealtimeCallController(
return BadRequest("Call session is not properly configured.");
var isAdmin = member.Role >= ChatMemberRole.Moderator;
var userToken = realtime.GetUserToken(currentUser, ongoingCall.SessionId, isAdmin);
// Get LiveKit endpoint from configuration
// Get WebRTC signaling server endpoint from configuration
var endpoint = _config.Endpoint ??
throw new InvalidOperationException("LiveKit endpoint configuration is missing");
throw new InvalidOperationException("WebRTC signaling endpoint configuration is missing");
// Inject the ChatRoomService
var chatRoomService = HttpContext.RequestServices.GetRequiredService<ChatRoomService>();
// Get current participants from the LiveKit service
// Get current participants from the participant list
var participants = new List<CallParticipant>();
if (realtime is LiveKitRealtimeService livekitService)
var roomKey = ongoingCall.RoomId.ToString();
if (RoomParticipants.TryGetValue(roomKey, out var partsDict))
{
var roomParticipants = await livekitService.GetRoomParticipantsAsync(ongoingCall.SessionId);
participants = [];
foreach (var p in roomParticipants)
{
var participant = new CallParticipant
participants.AddRange(from part in partsDict.Values
select new CallParticipant
{
Identity = p.Identity,
Name = p.Name,
AccountId = p.AccountId,
JoinedAt = p.JoinedAt
};
// Fetch the ChatMember profile if we have an account ID
if (p.AccountId.HasValue)
participant.Profile = await chatRoomService.GetRoomMember(p.AccountId.Value, roomId);
participants.Add(participant);
}
Identity = part.Account.Id,
Name = part.Account.Name,
AccountId = Guid.Parse(part.Account.Id),
JoinedAt = part.JoinedAt
});
}
// Create the response model
// Create the response model for built-in WebRTC signaling
var response = new JoinCallResponse
{
Provider = realtime.ProviderName,
Provider = "Built-in WebRTC Signaling",
Endpoint = endpoint,
Token = userToken,
Token = "", // No external token needed for built-in signaling
CallId = ongoingCall.Id,
RoomName = ongoingCall.SessionId,
IsAdmin = isAdmin,
@@ -186,6 +187,205 @@ public class RealtimeCallController(
return BadRequest(exception.Message);
}
}
/// <summary>
/// WebSocket signaling endpoint for WebRTC calls in a specific chat room.
/// Path: /api/chat/realtime/{chatId}
/// Requires JWT authentication (handled by middleware).
/// </summary>
[HttpGet("{chatId:guid}")]
public async Task SignalingWebSocket(Guid chatId)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser)
{
HttpContext.Response.StatusCode = 401;
await HttpContext.Response.WriteAsync("Unauthorized");
return;
}
// Verify the user is a member of the chat room
var accountId = Guid.Parse(currentUser.Id);
var member = await db.ChatMembers
.Where(m => m.AccountId == accountId && m.ChatRoomId == chatId && m.JoinedAt != null && m.LeaveAt == null)
.FirstOrDefaultAsync();
if (member == null || member.Role < ChatMemberRole.Member)
{
HttpContext.Response.StatusCode = 403;
await HttpContext.Response.WriteAsync("Forbidden: Not a member of this chat room");
return;
}
if (!HttpContext.WebSockets.IsWebSocketRequest)
{
HttpContext.Response.StatusCode = 400;
await HttpContext.Response.WriteAsync("Bad Request: WebSocket connection expected");
return;
}
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
var clientId = Guid.NewGuid();
// Add a client to the room-specific clients dictionary
var roomKey = chatId.ToString();
var roomDict = RoomClients.GetOrAdd(roomKey,
_ => new ConcurrentDictionary<Guid, (WebSocket, string, int)>());
roomDict.TryAdd(clientId, (webSocket, currentUser.Id, member.Role));
// Add to the participant list
var participantsDict = RoomParticipants.GetOrAdd(roomKey,
_ => new ConcurrentDictionary<string, (Account Account, DateTime JoinedAt)>());
var wasAdded = participantsDict.TryAdd(currentUser.Id, (currentUser, DateTime.UtcNow));
logger.LogInformation(
"WebRTC signaling client connected: {ClientId} ({UserId}) in room {RoomId}. Total clients in room: {Count}",
clientId, currentUser.Id, chatId, roomDict.Count);
// Get other participants as CallParticipant objects
var otherParticipants = participantsDict.Values
.Where(p => p.Account.Id != currentUser.Id)
.Select(p => new CallParticipant
{
Identity = p.Account.Id,
Name = p.Account.Name,
AccountId = Guid.Parse(p.Account.Id),
Account = SnAccount.FromProtoValue(p.Account),
JoinedAt = p.JoinedAt
})
.ToList();
var welcomePacket = new WebSocketPacket
{
Type = "webrtc",
Data = new
{
userId = currentUser.Id,
roomId = chatId,
message = $"Connected to call of #{chatId}.",
timestamp = DateTime.UtcNow.ToString("o"),
participants = otherParticipants
}
};
var responseBytes = welcomePacket.ToBytes();
await webSocket.SendAsync(new ArraySegment<byte>(responseBytes), WebSocketMessageType.Text, true,
CancellationToken.None);
// Broadcast user-joined to existing clients if this is the first connection for this user in the room
if (wasAdded)
{
var joinPacket = new WebSocketPacket
{
Type = "webrtc.signal",
Data = new SignalingMessage
{
Type = "user-joined",
AccountId = currentUser.Id,
Account = SnAccount.FromProtoValue(currentUser),
Data = new { }
}
};
await BroadcastMessageToRoom(chatId, clientId, joinPacket);
}
try
{
// Use a MemoryStream to build the full message from potentially multiple chunks.
using var ms = new MemoryStream();
// A larger buffer can be more efficient, but the loop is what handles correctness.
var buffer = new byte[1024 * 8];
while (webSocket.State == WebSocketState.Open)
{
ms.SetLength(0); // Clear the stream for the new message.
WebSocketReceiveResult result;
do
{
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
break;
}
ms.Write(buffer, 0, result.Count);
} while (!result.EndOfMessage);
if (result.MessageType == WebSocketMessageType.Close)
break;
var packet = WebSocketPacket.FromBytes(ms.ToArray());
var signalingMessage = packet.GetData<SignalingMessage>();
if (signalingMessage is null)
{
logger.LogWarning("Signaling message could not be parsed, dismissed...");
continue;
}
signalingMessage.AccountId = currentUser.Id;
signalingMessage.Account = SnAccount.FromProtoValue(currentUser);
var broadcastPacket = new WebSocketPacket
{
Type = "webrtc.signal",
Data = signalingMessage
};
logger.LogDebug("Message received from {ClientId} ({UserId}): Type={MessageType}", clientId, currentUser.Id, signalingMessage.Type);
await BroadcastMessageToRoom(chatId, clientId, broadcastPacket);
}
}
catch (WebSocketException wsex) when (wsex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely)
{
// This is an expected exception when a client closes the browser tab.
logger.LogDebug("WebRTC signaling client connection was closed prematurely for user {UserId}",
currentUser.Id);
}
catch (Exception ex)
{
logger.LogError(ex, "Error with WebRTC signaling client connection for user {UserId}", currentUser.Id);
}
finally
{
// Remove the client from the room
if (roomDict.TryRemove(clientId, out _))
{
logger.LogInformation(
"WebRTC signaling client disconnected: {ClientId} ({UserId}). Total clients in room: {Count}",
clientId, currentUser.Id, roomDict.Count);
// If no more connections from this account, remove from participants
if (roomDict.Values.All(v => v.AccountId != currentUser.Id))
{
var tempParticipantsDict = RoomParticipants.GetOrAdd(roomKey,
_ => new ConcurrentDictionary<string, (Account Account, DateTime JoinedAt)>());
if (tempParticipantsDict.TryRemove(currentUser.Id, out _))
{
logger.LogInformation("Participant {UserId} removed from room {RoomId}", currentUser.Id,
chatId);
}
}
}
webSocket.Dispose();
}
}
private async Task BroadcastMessageToRoom(Guid roomId, Guid senderId, WebSocketPacket packet)
{
var roomKey = roomId.ToString();
if (!RoomClients.TryGetValue(roomKey, out var roomDict))
return;
var messageBytes = packet.ToBytes();
var segment = new ArraySegment<byte>(messageBytes);
foreach (var pair in roomDict)
{
if (pair.Key == senderId) continue;
if (pair.Value.Socket.State != WebSocketState.Open) continue;
await pair.Value.Socket.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None);
logger.LogDebug("Message broadcasted to {ClientId} in room {RoomId}", pair.Key, roomId);
}
}
}
// Response model for joining a call
@@ -220,7 +420,7 @@ public class JoinCallResponse
/// Whether the user is the admin of the call
/// </summary>
public bool IsAdmin { get; set; }
/// <summary>
/// Current participants in the call
/// </summary>
@@ -236,22 +436,22 @@ public class CallParticipant
/// The participant's identity (username)
/// </summary>
public string Identity { get; set; } = null!;
/// <summary>
/// The participant's display name
/// </summary>
public string Name { get; set; } = null!;
/// <summary>
/// The participant's account ID if available
/// </summary>
public Guid? AccountId { get; set; }
/// <summary>
/// The participant's profile in the chat
/// </summary>
public SnChatMember? Profile { get; set; }
public SnAccount? Account { get; set; }
/// <summary>
/// When the participant joined the call
/// </summary>

View File

@@ -4,7 +4,7 @@
"SiteUrl": "https://solian.app",
"Logging": {
"LogLevel": {
"Default": "Information",
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},

View File

@@ -1,38 +1,38 @@
{
"Debug": true,
"BaseUrl": "http://localhost:5071",
"SiteUrl": "https://solian.app",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Debug": true,
"BaseUrl": "http://localhost:5071",
"SiteUrl": "https://solian.app",
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"App": "Host=host.docker.internal;Port=5432;Database=dyson_network;Username=postgres;Password=postgres;Include Error Detail=True;Maximum Pool Size=20;Connection Idle Lifetime=60"
},
"GeoIp": {
"DatabasePath": "/app/keys/GeoLite2-City.mmdb"
},
"RealtimeChat": {
"Endpoint": "https://solar-network-im44o8gq.livekit.cloud",
"ApiKey": "",
"ApiSecret": ""
},
"Translation": {
"Provider": "Tencent",
"Region": "ap-hongkong",
"ProjectId": "0",
"SecretId": "",
"SecretKey": ""
},
"KnownProxies": ["127.0.0.1", "::1"],
"Etcd": {
"Insecure": true
},
"Service": {
"Name": "DysonNetwork.Sphere",
"Url": "https://localhost:7099"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"App": "Host=host.docker.internal;Port=5432;Database=dyson_network;Username=postgres;Password=postgres;Include Error Detail=True;Maximum Pool Size=20;Connection Idle Lifetime=60"
},
"GeoIp": {
"DatabasePath": "/app/keys/GeoLite2-City.mmdb"
},
"RealtimeChat": {
"Endpoint": "https://solar-network-im44o8gq.livekit.cloud",
"ApiKey": "",
"ApiSecret": ""
},
"Translation": {
"Provider": "Tencent",
"Region": "ap-hongkong",
"ProjectId": "0",
"SecretId": "",
"SecretKey": ""
},
"KnownProxies": ["127.0.0.1", "::1"],
"Etcd": {
"Insecure": true
},
"Service": {
"Name": "DysonNetwork.Sphere",
"Url": "https://localhost:7099"
}
}