diff --git a/src/Crash.Common/Communications/CrashClient.cs b/src/Crash.Common/Communications/CrashClient.cs index 610db08..72ecb08 100644 --- a/src/Crash.Common/Communications/CrashClient.cs +++ b/src/Crash.Common/Communications/CrashClient.cs @@ -1,9 +1,11 @@ using System.Diagnostics; +using System.Text.Json; using Crash.Common.Document; using Crash.Common.Events; using Crash.Common.Exceptions; using Crash.Common.Logging; +using Crash.Common.Tables; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR.Client; @@ -27,6 +29,7 @@ public sealed class CrashClient const string UNSELECT = "Unselect"; const string INITIALIZE = "Initialize"; const string CAMERACHANGE = "CameraChange"; + const string UPDATEUSER = "UpdateUser"; // TODO : Move to https public const string DefaultURL = "http://localhost"; @@ -56,6 +59,7 @@ public event Func Closed public event Action OnUnselect; public event Action OnInitialize; public event Action OnCameraChange; + public event Action OnUpdateUser; /// /// Stop async task @@ -87,6 +91,17 @@ public CrashClient(CrashDoc crashDoc, string userName, Uri url) _user = userName; _connection = GetHubConnection(url); RegisterConnections(); + + UserTable.OnUserAdded += (sender, userArgs) => + { + string json = JsonSerializer.Serialize(userArgs.User); + Change userChange = new Change(Guid.NewGuid(), userName, json) + { + Action = ChangeAction.Update, + }; + + UpdateUserAsync(userChange); + }; } internal static HubConnection GetHubConnection(Uri url) => new HubConnectionBuilder() @@ -127,6 +142,7 @@ internal void RegisterConnections() _connection.On(UNSELECT, (user, id) => OnUnselect?.Invoke(user, id)); _connection.On(INITIALIZE, (Changes) => OnInitialize?.Invoke(Changes)); _connection.On(CAMERACHANGE, (user, Change) => OnCameraChange?.Invoke(user, Change)); + _connection.On(UPDATEUSER, (Change) => OnUpdateUser?.Invoke(Change)); _connection.Reconnected += ConnectionReconnectedAsync; _connection.Closed += ConnectionClosedAsync; @@ -246,7 +262,13 @@ public async Task UnselectAsync(Guid id) /// public async Task CameraChangeAsync(Change Change) { - await _connection.InvokeAsync(CAMERACHANGE, _user, Change); + await _connection.InvokeAsync(CAMERACHANGE, Change.Owner, Change); + } + + /// Updates a User + public async Task UpdateUserAsync(Change Change) + { + await _connection.InvokeAsync(UPDATEUSER, Change); } ///