Skip to content
This repository was archived by the owner on Apr 23, 2023. It is now read-only.
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Crash.Common/Communications/CrashClient.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -56,6 +59,7 @@ public event Func<Exception, Task> Closed
public event Action<string, Guid> OnUnselect;
public event Action<Change[]> OnInitialize;
public event Action<string, Change> OnCameraChange;
public event Action<Change> OnUpdateUser;

/// <summary>
/// Stop async task
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -127,6 +142,7 @@ internal void RegisterConnections()
_connection.On<string, Guid>(UNSELECT, (user, id) => OnUnselect?.Invoke(user, id));
_connection.On<Change[]>(INITIALIZE, (Changes) => OnInitialize?.Invoke(Changes));
_connection.On<string, Change>(CAMERACHANGE, (user, Change) => OnCameraChange?.Invoke(user, Change));
_connection.On<Change>(UPDATEUSER, (Change) => OnUpdateUser?.Invoke(Change));

_connection.Reconnected += ConnectionReconnectedAsync;
_connection.Closed += ConnectionClosedAsync;
Expand Down Expand Up @@ -246,7 +262,13 @@ public async Task UnselectAsync(Guid id)
/// <returns></returns>
public async Task CameraChangeAsync(Change Change)
{
await _connection.InvokeAsync(CAMERACHANGE, _user, Change);
await _connection.InvokeAsync(CAMERACHANGE, Change.Owner, Change);
}

/// <summary>Updates a User</summary>
public async Task UpdateUserAsync(Change Change)
{
await _connection.InvokeAsync(UPDATEUSER, Change);
}

/// <summary>
Expand Down