Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signalR fles added #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Binary file not shown.
991 changes: 991 additions & 0 deletions ChatApp_SignalR/.vs/ChatApp_SignalR/config/applicationhost.config

Large diffs are not rendered by default.

Binary file added ChatApp_SignalR/.vs/ChatApp_SignalR/v16/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
25 changes: 25 additions & 0 deletions ChatApp_SignalR/ChatApp_SignalR.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatApp_SignalR", "ChatApp_SignalR\ChatApp_SignalR.csproj", "{26C6A5F4-E55F-4BD8-842F-C2DF84E92120}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{26C6A5F4-E55F-4BD8-842F-C2DF84E92120}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26C6A5F4-E55F-4BD8-842F-C2DF84E92120}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26C6A5F4-E55F-4BD8-842F-C2DF84E92120}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26C6A5F4-E55F-4BD8-842F-C2DF84E92120}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0FC9EA28-6727-4A22-9C2B-2422B8A382A4}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "/Views/Shared/_Layout.cshtml";
}
19 changes: 19 additions & 0 deletions ChatApp_SignalR/ChatApp_SignalR/ChatApp_SignalR.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>aspnet-ChatApp_SignalR-AC83A05F-623A-4533-A173-D0A86E8C36A6</UserSecretsId>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions ChatApp_SignalR/ChatApp_SignalR/ChatApp_SignalR.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WebStackScaffolding_ViewDialogWidth>600</WebStackScaffolding_ViewDialogWidth>
<WebStackScaffolding_IsLayoutPageSelected>True</WebStackScaffolding_IsLayoutPageSelected>
<WebStackScaffolding_IsPartialViewSelected>False</WebStackScaffolding_IsPartialViewSelected>
<WebStackScaffolding_IsReferencingScriptLibrariesSelected>False</WebStackScaffolding_IsReferencingScriptLibrariesSelected>
<WebStackScaffolding_LayoutPageFile />
</PropertyGroup>
</Project>
14 changes: 14 additions & 0 deletions ChatApp_SignalR/ChatApp_SignalR/ChatUserViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ChatApp_SignalR
{
public class ChatUserViewModel
{
public string UserId { get; set; }
public string UserName { get; set; }
public DateTime MessageTime { get; set; }
}
}
107 changes: 107 additions & 0 deletions ChatApp_SignalR/ChatApp_SignalR/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ChatApp_SignalR.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using ChatApp_SignalR.Data;

namespace ChatApp_SignalR.Controllers
{
[Authorize]
public class ChatController : Controller
{
private readonly ILogger<ChatController> _logger;
public ApplicationDbContext dbContext = null;
UserManager<ApplicationUser> _userManager;
private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();



public ChatController(ILogger<ChatController> logger, ApplicationDbContext _dbContext,
UserManager<ApplicationUser> userManager)
{
dbContext = _dbContext;
_userManager = userManager;
_logger = logger;
}

public IActionResult Index()
{

return View("Chat");
}

public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}


[HttpGet]
public JsonResult getLoggedInUserId()
{
string LoggedInUserId = _userManager.GetUserId(HttpContext.User);
return Json(LoggedInUserId);
}

public JsonResult LoadAllUsers()
{

var LoggedInUserId = _userManager.GetUserId(HttpContext.User);
var userList = dbContext.Users.Where(s => s.Id != LoggedInUserId).OrderBy(s => s.UserName);
return Json(userList);
}

[HttpGet]
public JsonResult getCountofUnreadMessages(string Id)
{
string LoggedInUserId = _userManager.GetUserId(HttpContext.User);
var unreadMessageCount = dbContext.Chat.Where(s => s.isRead == false && s.SenderId == Id && s.ReceiverId == LoggedInUserId)
.Count();
return Json(unreadMessageCount);
}
public JsonResult LoadChatDetail(string Id)
{
string LoggedInUserId = _userManager.GetUserId(HttpContext.User);

List<Chat> chatModel = (from p in dbContext.Chat
where p.SenderId == Id && p.ReceiverId == LoggedInUserId
select p).ToList();

foreach (Chat p in chatModel)
{
p.isRead = true;
}
dbContext.SaveChanges();

var chatDetail = dbContext.Chat.Select(s => new MessageViewModel
{
MessageId = s.ChatId,
MessageBody = s.MessageBody,
SenderId = s.SenderId,
SenderName = s.SenderUser.UserName,
ReceiverId = s.ReceiverId,
ReceiverName = s.ReceiverUser.UserName,
DateCreateed = s.DateCreateed,
//messageFrom = LoggedInUserId == s.SenderId? "sender": "receiver"
//messageFrom = HttpContext.User.Identity.Name
messageFrom = _userManager.FindByIdAsync(s.SenderId).Result.UserName
})
.Where(s => s.SenderId == LoggedInUserId && s.ReceiverId == Id || s.ReceiverId == LoggedInUserId && s.SenderId == Id)
.OrderBy(s => s.DateCreateed)
.ToList();
return Json(chatDetail);
}
}
}
19 changes: 19 additions & 0 deletions ChatApp_SignalR/ChatApp_SignalR/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using ChatApp_SignalR.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace ChatApp_SignalR.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

public DbSet<Chat> Chat { get; set; }
}
}
98 changes: 98 additions & 0 deletions ChatApp_SignalR/ChatApp_SignalR/Hubs/ChatHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using ChatApp_SignalR.Data;
using ChatApp_SignalR.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace ChatApp_SignalR.Hubs
{
public class ChatHub : Hub
{
private ApplicationDbContext _db;
private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();
UserManager<ApplicationUser> _userManager;
private readonly IHttpContextAccessor _httpContextAccessor;

public ChatHub(ApplicationDbContext db,
UserManager<ApplicationUser> userManager,
IHttpContextAccessor httpContextAccessor)
{
_db = db;
_userManager = userManager;
_httpContextAccessor = httpContextAccessor;
}

public async Task SendMessage(MessageViewModel messageViewModel)
{
//await Clients.All.SendAsync("ReceiveMessage", user, message);


string LoggedInUserId = _userManager.GetUserId(_httpContextAccessor.HttpContext.User);
messageViewModel.DateCreateed = DateTime.Now;
messageViewModel.isRead = false;
try
{
_db.Chat.Add(new Chat
{
MessageBody = messageViewModel.MessageBody,
ReceiverId = messageViewModel.ReceiverId,
SenderId = messageViewModel.SenderId,
DateCreateed = messageViewModel.DateCreateed,
isRead = messageViewModel.isRead,
});
_db.SaveChanges();
var receiverconnectedId = _connections.GetConnections(messageViewModel.ReceiverId).FirstOrDefault();
var senderconnectedId = _connections.GetConnections(messageViewModel.SenderId).FirstOrDefault();

var messageModel = new MessageViewModel
{
MessageBody = messageViewModel.MessageBody,
SenderId = messageViewModel.SenderId,
ReceiverId = messageViewModel.ReceiverId,
DateCreateed = messageViewModel.DateCreateed,
messageFrom = _httpContextAccessor.HttpContext.User.Identity.Name
};

//if (LoggedInUserId == messageModel.ReceiverId)
// messageModel.messageFrom = "receiver";
//if (LoggedInUserId == messageModel.SenderId)
// messageModel.messageFrom = "sender";






await Clients.Clients(receiverconnectedId, senderconnectedId).SendAsync("ReceiveMessage", messageModel);
//await Clients.All.SendAsync("ReceiveMessage", user, message);

}
catch (Exception e)
{

}
}

public override Task OnConnectedAsync()
{
string name = _db.Users.Where(s => s.UserName == Context.User.Identity.Name).Select(s => s.Id).FirstOrDefault();
_connections.Add(name, Context.ConnectionId);
var connectedUsers = _connections.GetAllActiveConnections();
Clients.All.SendAsync("GetAllConnections", connectedUsers);
return base.OnConnectedAsync();
}

public override Task OnDisconnectedAsync(Exception exception)
{
string name = _db.Users.Where(s => s.UserName == Context.User.Identity.Name).Select(s => s.Id).FirstOrDefault();
_connections.Remove(name, Context.ConnectionId);
var offlineUsers = _connections.GetAllActiveConnections();
Clients.All.SendAsync("GetAllConnections", offlineUsers);
return base.OnDisconnectedAsync(exception);
}
}
}
Loading