Skip to content

Commit

Permalink
(#394) web: communication add page reload in teh case the connection …
Browse files Browse the repository at this point in the history
…was not set up
  • Loading branch information
SaintAngeLs committed Aug 29, 2024
1 parent 93b4a86 commit 5489c28
Show file tree
Hide file tree
Showing 3 changed files with 385 additions and 318 deletions.
273 changes: 136 additions & 137 deletions MiniSpace.Web/src/MiniSpace.Web/Areas/Communication/ChatSignalRService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,172 +9,171 @@
namespace MiniSpace.Web.Areas.Communication
{
public class ChatSignalRService : IAsyncDisposable
{
private HubConnection _hubConnection;
private readonly NavigationManager _navigationManager;
private readonly IIdentityService _identityService;
private Guid _userId;
private Guid _currentChatId;
private bool _disposed;

public event Action<MessageDto> MessageReceived;
public event Action<Guid, string> MessageStatusUpdated;
public event Action<string, bool> TypingNotificationReceived;
public event Action<bool> ConnectionChanged;

public ChatSignalRService(NavigationManager navigationManager, IIdentityService identityService)
{
_navigationManager = navigationManager;
_identityService = identityService;
}

public async Task StartAsync(Guid userId, Guid currentChatId)
{
_userId = userId;
_currentChatId = currentChatId;
var hubUrl = $"http://localhost:5016/chatHub?userId={userId}&chatId={currentChatId}";
private HubConnection _hubConnection;
private readonly NavigationManager _navigationManager;
private readonly IIdentityService _identityService;
private Guid _userId;
private Guid _currentChatId;
private bool _disposed;

public event Action<MessageDto> MessageReceived;
public event Action<Guid, string> MessageStatusUpdated;
public event Action<string, bool> TypingNotificationReceived;
public event Action<bool> ConnectionChanged;

public ChatSignalRService(NavigationManager navigationManager, IIdentityService identityService)
{
_navigationManager = navigationManager;
_identityService = identityService;
}

_hubConnection = new HubConnectionBuilder()
.WithUrl(hubUrl, options =>
public async Task StartAsync(Guid userId, Guid currentChatId)
{
if (_hubConnection != null && _hubConnection.State == HubConnectionState.Connected)
{
options.AccessTokenProvider = async () =>
{
if (_disposed)
{
return null; // Circuit is disposed, return null to avoid further calls.
}
await _hubConnection.StopAsync();
}

try
{
var token = await _identityService.GetAccessTokenAsync();
return token;
}
catch (JSDisconnectedException)
_userId = userId;
_currentChatId = currentChatId;
var hubUrl = $"http://localhost:5016/chatHub?userId={userId}&chatId={currentChatId}";

_hubConnection = new HubConnectionBuilder()
.WithUrl(hubUrl, options =>
{
options.AccessTokenProvider = async () =>
{
// Handle the JS interop disconnection gracefully
return null;
}
};
})
.WithAutomaticReconnect()
.Build();

_hubConnection.On<string>("ReceiveMessage", (jsonMessage) =>
{
var message = System.Text.Json.JsonSerializer.Deserialize<MessageDto>(jsonMessage);
MessageReceived?.Invoke(message);
});
if (_disposed)
{
return null;
}
try
{
return await _identityService.GetAccessTokenAsync();
}
catch (JSDisconnectedException)
{
return null;
}
};
})
.WithAutomaticReconnect()
.Build();

RegisterHubEvents();
await StartConnectionAsync();
}

_hubConnection.On<string>("ReceiveMessageStatusUpdate", (jsonStatusUpdate) =>
public async Task StartAsync(Guid userId)
{
var statusUpdate = System.Text.Json.JsonSerializer.Deserialize<MessageStatusUpdateDto>(jsonStatusUpdate);
var chatId = statusUpdate.ChatId;
var messageId = Guid.Parse(statusUpdate.MessageId);
var status = statusUpdate.Status;
if (chatId == _currentChatId.ToString())
if (_hubConnection != null && _hubConnection.State == HubConnectionState.Connected)
{
MessageStatusUpdated?.Invoke(messageId, status);
await _hubConnection.StopAsync();
}
});

_hubConnection.On<string, bool>("ReceiveTypingNotification", (userId, isTyping) =>
{
TypingNotificationReceived?.Invoke(userId, isTyping);
});
_userId = userId;
_currentChatId = Guid.Empty;
var hubUrl = $"http://localhost:5016/chatHub?userId={userId}";

_hubConnection.Reconnecting += (error) =>
{
ConnectionChanged?.Invoke(false);
return Task.CompletedTask;
};
_hubConnection = new HubConnectionBuilder()
.WithUrl(hubUrl, options =>
{
options.AccessTokenProvider = async () =>
{
if (_disposed)
{
return null;
}
try
{
return await _identityService.GetAccessTokenAsync();
}
catch (JSDisconnectedException)
{
return null;
}
};
})
.WithAutomaticReconnect()
.Build();

RegisterHubEvents();
await StartConnectionAsync();
}

_hubConnection.Reconnected += (connectionId) =>
private void RegisterHubEvents()
{
ConnectionChanged?.Invoke(true);
return Task.CompletedTask;
};
_hubConnection.On<string>("ReceiveMessage", (jsonMessage) =>
{
var message = System.Text.Json.JsonSerializer.Deserialize<MessageDto>(jsonMessage);
MessageReceived?.Invoke(message);
});

_hubConnection.Closed += (error) =>
{
ConnectionChanged?.Invoke(false);
return Task.CompletedTask;
};
_hubConnection.On<string>("ReceiveMessageStatusUpdate", (jsonStatusUpdate) =>
{
var statusUpdate = System.Text.Json.JsonSerializer.Deserialize<MessageStatusUpdateDto>(jsonStatusUpdate);
MessageStatusUpdated?.Invoke(Guid.Parse(statusUpdate.MessageId), statusUpdate.Status);
});

if (!_disposed)
{
await _hubConnection.StartAsync();
ConnectionChanged?.Invoke(true);
}
}
_hubConnection.On<string, bool>("ReceiveTypingNotification", (userId, isTyping) =>
{
TypingNotificationReceived?.Invoke(userId, isTyping);
});

public async Task StartAsync(Guid userId)
{
_userId = userId;
_currentChatId = Guid.Empty; // No specific chat in this context
var hubUrl = $"http://localhost:5016/chatHub?userId={userId}";
_hubConnection.Reconnecting += (error) =>
{
ConnectionChanged?.Invoke(false);
return Task.CompletedTask;
};

_hubConnection = new HubConnectionBuilder()
.WithUrl(hubUrl, options =>
_hubConnection.Reconnected += (connectionId) =>
{
options.AccessTokenProvider = async () =>
{
if (_disposed)
{
return null; // Circuit is disposed, return null to avoid further calls.
}
ConnectionChanged?.Invoke(true);
return Task.CompletedTask;
};

try
{
var token = await _identityService.GetAccessTokenAsync();
return token;
}
catch (JSDisconnectedException)
{
// Handle the JS interop disconnection gracefully
return null;
}
};
})
.WithAutomaticReconnect()
.Build();

_hubConnection.On<string>("ReceiveMessage", (jsonMessage) =>
{
var message = System.Text.Json.JsonSerializer.Deserialize<MessageDto>(jsonMessage);
MessageReceived?.Invoke(message);
});
_hubConnection.Closed += (error) =>
{
ConnectionChanged?.Invoke(false);
return Task.CompletedTask;
};
}

_hubConnection.On<string, bool>("ReceiveTypingNotification", (userId, isTyping) =>
private async Task StartConnectionAsync()
{
TypingNotificationReceived?.Invoke(userId, isTyping);
});
if (!_disposed)
{
await _hubConnection.StartAsync();
ConnectionChanged?.Invoke(true);
}
}

if (!_disposed)
public async Task StopAsync()
{
await _hubConnection.StartAsync();
ConnectionChanged?.Invoke(true);
if (_hubConnection != null && _hubConnection.State != HubConnectionState.Disconnected)
{
await _hubConnection.StopAsync();
}
}
}


public async Task SendTypingNotificationAsync(bool isTyping)
{
if (_hubConnection.State == HubConnectionState.Connected)
public async Task SendTypingNotificationAsync(bool isTyping)
{
await _hubConnection.InvokeAsync("SendTypingNotification", _currentChatId.ToString(), _userId.ToString(), isTyping);
if (_hubConnection.State == HubConnectionState.Connected)
{
await _hubConnection.InvokeAsync("SendTypingNotification", _currentChatId.ToString(), _userId.ToString(), isTyping);
}
}
}

public async ValueTask DisposeAsync()
{
if (_hubConnection != null)
public async ValueTask DisposeAsync()
{
await _hubConnection.DisposeAsync();
if (_hubConnection != null)
{
await _hubConnection.DisposeAsync();
}
_disposed = true;
}
_disposed = true;
}
}


}
Loading

0 comments on commit 5489c28

Please sign in to comment.