Skip to content

Commit

Permalink
feat(issue-291): include seignalR to smalltalk mobile app
Browse files Browse the repository at this point in the history
  • Loading branch information
live-dev999 committed Apr 10, 2022
1 parent b60eacd commit 45ec648
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class GlobalSetting
{
public string GatewayChatEndpoint { get; set; } = "https://api-smalltalk.o2bus.com";
public static GlobalSetting Instance { get; } = new GlobalSetting();
public string HubConnectionURL { get; set; } = "http://localhost:5000/chathub";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="IdentityModel" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="7.0.0-preview.2.22153.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Xamarin.Forms" Version="5.0.0.2401" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public async Task<ChatMessage> AddMessageToSessionAsync(string message)
{
Id = 0,
Message = message,
RecipientId = 2,
SenderId = 1
RecipientId = 1,
SenderId = 2
});

return addMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using O2NextGen.Sdk.NetCore.Models.smalltalk;
using Microsoft.AspNetCore.SignalR.Client;
using O2NextGen.Sdk.NetCore.Models.smalltalk;
using O2NextGen.SmallTalk.Core.Services.Chat;
using O2NextGen.SmallTalk.Core.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
Expand All @@ -16,10 +18,13 @@ public class ChatDetailViewModel : ViewModelBase
private ChatSession session;
private ObservableCollection<ChatMessage> messages;
private string message;

#endregion

#region Commands
public ICommand SendMsgCommand { get; private set; }

private readonly HubConnection hubConnection;
#endregion

#region Props
Expand All @@ -33,6 +38,18 @@ public ChatDetailViewModel()
this.MultipleInitialization = true;
_chatService = DependencyService.Get<IChatService>();
SendMsgCommand = new Command(async (item) => await SendMsgAsync());

hubConnection = new HubConnectionBuilder()
.WithUrl(GlobalSetting.Instance.HubConnectionURL)
.Build();

hubConnection.On("OnUpdateMessage", async () =>
{
Console.WriteLine("TestMessage");
await RelaodData();
});


}

public ChatSession Session
Expand Down Expand Up @@ -68,8 +85,10 @@ public string Message
public override async Task InitializeAsync(IDictionary<string, string> query)
{

await RelaodData();


await hubConnection.StartAsync();
await hubConnection.InvokeAsync("NewUserAsync","Denis");

}

private async Task SendMsgAsync()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.AspNetCore.Mvc;
using O2NextGen.SmallTalk.SignalrHub.Hubs;

namespace O2NextGen.SmallTalk.SignalrHub.Controllers
{
[Route("api/chat")]
[ApiController]
public class ChatController : ControllerBase
{
private readonly IChatHub chatHub;

public ChatController(IChatHub chatHub)
{
this.chatHub = chatHub;
}
[HttpGet]
public void Test()
{
chatHub.UpdateMessages();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@

namespace O2NextGen.SmallTalk.SignalrHub.Hubs
{
public class ChatHub : Hub
public interface IChatHub {
Task UpdateMessages();
}
public class ChatHub : Hub, IChatHub
{
public override async Task OnConnectedAsync()
public async Task NewUserAsync(string username)
{
await Groups.AddToGroupAsync(Context.ConnectionId, Context.User.Identity.Name);
await Groups.AddToGroupAsync(Context.ConnectionId, username);
await base.OnConnectedAsync();
}

public override async Task OnDisconnectedAsync(Exception ex)
public async Task UpdateMessages()
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.User.Identity.Name);
await base.OnDisconnectedAsync(ex);
await Clients.All.SendAsync("OnUpdateMessage");
}

//public override async Task OnConnectedAsync()
//{
// await Groups.AddToGroupAsync(Context.ConnectionId, Context.User.Identity.Name);
// await base.OnConnectedAsync();
//}

//public override async Task OnDisconnectedAsync(Exception ex)
//{
// await Groups.RemoveFromGroupAsync(Context.ConnectionId, Context.User.Identity.Name);
// await base.OnDisconnectedAsync(ex);
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"O2NextGen.SmallTalk.SignalrHub": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand Down
22 changes: 19 additions & 3 deletions src/Services/smalltalk/O2NextGen.SmallTalk.SignalrHub/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using O2NextGen.SmallTalk.SignalrHub.Hubs;

namespace O2NextGen.SmallTalk.SignalrHub
{
Expand All @@ -19,6 +20,7 @@ public void ConfigureServices(IServiceCollection services)
.SetIsOriginAllowed((host) => true)
.AllowCredentials());
});
services.AddSingleton<IChatHub,ChatHub>();
services.AddSignalR();
}

Expand All @@ -29,13 +31,27 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
}

//else
//{
// app.UseHsts();
//}
app.UseCors("CorsPolicy");

//app.UseRouting();

app.UseAuthentication();
//app.UseAuthentication();
//app.UseAuthorization();

app.UseSignalR((routes) =>
{
routes.MapHub<ChatHub>("/chathub");
});
app.UseMvc();
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapControllers();
// endpoints.MapHub<SignalRtcHub>("/signalrtc");
// endpoints.MapHub<O2Hub>("/o2hub");
//});
//app.UseEndpoints(endpoints =>
//{
// endpoints.MapHub<NotificationsHub>("/hub/chathub",
Expand Down

0 comments on commit 45ec648

Please sign in to comment.