|
@@ -1,8 +1,6 @@
|
|
|
-
|
|
|
-using System;
|
|
|
-using Fleck;
|
|
|
+using Fleck;
|
|
|
using System.Collections.Concurrent;
|
|
|
-using LitJson;
|
|
|
+using System.Text.Json;
|
|
|
|
|
|
namespace VRTC
|
|
|
{
|
|
@@ -47,7 +45,7 @@ namespace VRTC
|
|
|
/// <returns>TODO reactor later</returns>
|
|
|
/// <exception cref="NotSupportedException">should be larger than 0</exception>
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
|
- string Join(uint roomId, string clientId, IWebSocketConnection webSocketConnection);
|
|
|
+ void Join(uint roomId, string clientId, IWebSocketConnection webSocketConnection);
|
|
|
|
|
|
/// <summary>
|
|
|
/// Exit from room when socket disconnected
|
|
@@ -94,13 +92,20 @@ namespace VRTC
|
|
|
{
|
|
|
if(_connectionClients.ContainsKey(clientConnectionId))
|
|
|
{
|
|
|
- JsonData data = new JsonData();
|
|
|
- data["command"] = "OnSuccessAnswer";
|
|
|
- data["sdp"] = sdp;
|
|
|
var clientId = _connectionClients[clientConnectionId];
|
|
|
- data["clientId"] = clientId;
|
|
|
- ForwardToOther(roomId, data.ToJson(), targetClientId);
|
|
|
- }
|
|
|
+ string msg = CreateSdpMessage("OnSuccessAnswer", clientId, sdp);
|
|
|
+ ForwardToOther(roomId, msg, targetClientId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private string CreateSdpMessage(string command, string clientId, string sdp)
|
|
|
+ {
|
|
|
+ var data = new WebSocketServerSdpMessage();
|
|
|
+ data.Command = command;
|
|
|
+ data.Sdp = sdp;
|
|
|
+ data.ClientId = clientId;
|
|
|
+ var msg = JsonSerializer.Serialize(data);
|
|
|
+ return msg;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -122,32 +127,41 @@ namespace VRTC
|
|
|
}
|
|
|
|
|
|
if (_connectionClients.ContainsKey(clientConnectionId))
|
|
|
- {
|
|
|
- JsonData jsonData = new JsonData();
|
|
|
- jsonData["command"] = "OnOffer";
|
|
|
+ {
|
|
|
var clientId = _connectionClients[clientConnectionId];
|
|
|
- jsonData["clientId"] = clientId;
|
|
|
- jsonData["sdp"] = sdp;
|
|
|
- ForwardToOther(roomId, jsonData.ToJson(), targetClientId);
|
|
|
+ string msg = CreateSdpMessage("OnOffer", clientId, sdp);
|
|
|
+ ForwardToOther(roomId, msg, targetClientId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void ForwardCandidate(uint roomId, Guid clientConnectionId, string candidate, string sdpMid, int sdpMLineIndex, string targetClientId)
|
|
|
{
|
|
|
if (_connectionClients.ContainsKey(clientConnectionId))
|
|
|
- {
|
|
|
- JsonData data = new JsonData();
|
|
|
- data["command"] = "OnIceCandidate";
|
|
|
- data["sdpMid"] = sdpMid;
|
|
|
- data["sdpMLineIndex"] = sdpMLineIndex;
|
|
|
- data["sdp"] = candidate;
|
|
|
+ {
|
|
|
var clientId = _connectionClients[clientConnectionId];
|
|
|
- data["clientId"] = clientId;
|
|
|
- ForwardToOther(roomId, data.ToJson(), targetClientId);
|
|
|
+ var data = new WebSocketServerCandidateMessage();
|
|
|
+ data.Command = "OnIceCandidate";
|
|
|
+ data.SdpMid = sdpMid;
|
|
|
+ data.SdpMlineIndex = sdpMLineIndex;
|
|
|
+ data.Candidate = candidate;
|
|
|
+ data.ClientId = clientId;
|
|
|
+
|
|
|
+ var msg = JsonSerializer.Serialize(data);
|
|
|
+ ForwardToOther(roomId, msg, targetClientId);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
+ private string CreateMessage(string command, string clientId, JoinState state)
|
|
|
+ {
|
|
|
+ var data = new WebSocketServerMessage();
|
|
|
+ data.Command = command;
|
|
|
+ data.State = state;
|
|
|
+ data.ClientId = clientId;
|
|
|
+ var msg = JsonSerializer.Serialize(data);
|
|
|
+ return msg;
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
///Join a room, create a new room of not exist
|
|
|
/// </summary>
|
|
@@ -156,7 +170,7 @@ namespace VRTC
|
|
|
/// <returns>TODO reactor later</returns>
|
|
|
/// <exception cref="NotSupportedException">should be larger than 0</exception>
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
|
- public string Join(uint roomId, string clientId, IWebSocketConnection webSocketConnection)
|
|
|
+ public void Join(uint roomId, string clientId, IWebSocketConnection webSocketConnection)
|
|
|
{
|
|
|
if (roomId <= 0)
|
|
|
{
|
|
@@ -166,34 +180,32 @@ namespace VRTC
|
|
|
{
|
|
|
throw new ArgumentNullException($"Client ID is empty!");
|
|
|
}
|
|
|
- JsonData jsonData = new JsonData();
|
|
|
- jsonData["command"] = "JoinResult";
|
|
|
+
|
|
|
+ var command = "JoinResult";
|
|
|
|
|
|
var room = _rooms.AddOrUpdate(roomId, (id) => {
|
|
|
var room = new WebRtcRoom(id, clientId);
|
|
|
room.Closed += OnRoomClosed;
|
|
|
room.Join(clientId, webSocketConnection);
|
|
|
- jsonData["state"] = "RoomOpened";
|
|
|
- webSocketConnection.Send(jsonData.ToJson());
|
|
|
+
|
|
|
+ var jsonData = CreateMessage(command, clientId, JoinState.RoomOpened);
|
|
|
+ webSocketConnection.Send(jsonData);
|
|
|
return room;
|
|
|
}, (id, exist) => {
|
|
|
- jsonData["state"] = "RoomJoined";
|
|
|
+
|
|
|
exist.Join(clientId, webSocketConnection);
|
|
|
- webSocketConnection.Send(jsonData.ToJson());
|
|
|
+ var jsonData = CreateMessage(command, clientId, JoinState.RoomJoined);
|
|
|
+ webSocketConnection.Send(jsonData);
|
|
|
|
|
|
- JsonData jsonDataForward = new JsonData();
|
|
|
- jsonDataForward["command"] = "OnJoin";
|
|
|
- jsonDataForward["clientId"] = clientId;
|
|
|
-
|
|
|
- exist.BroadcastToOthers(clientId, jsonDataForward.ToJson());
|
|
|
+ var jsonDataForward = CreateMessage("OnJoin", clientId, JoinState.RoomJoined);
|
|
|
+
|
|
|
+ exist.BroadcastToOthers(clientId, jsonDataForward);
|
|
|
|
|
|
return exist;
|
|
|
});
|
|
|
|
|
|
- Console.WriteLine($"{clientId} {jsonData["command"]} {roomId}");
|
|
|
- _connectionClients[webSocketConnection.ConnectionInfo.Id] = clientId;
|
|
|
-
|
|
|
- return jsonData.ToJson();
|
|
|
+
|
|
|
+ _connectionClients[webSocketConnection.ConnectionInfo.Id] = clientId;
|
|
|
}
|
|
|
|
|
|
private void OnRoomClosed(object? sender, EventArgs e)
|