Ravi Birla

Ravi Birla

  • NA
  • 292
  • 20.1k

Reconnect in signalR

Dec 29 2017 2:43 AM
Hi Everyone,
 
I am working on SignalR chat application.Please help me on below mention topics.
 
1. I want to hide(remove) users details from disconnected chat room once user is online. Here When a user diconnect the chatroom it is removed from connected user list and shows in disconnected users list but if user recconect with hub then it is appear in both list Connected user list and disconnected users list. But it should not happen so, I want to remove that users details from disconnected list after user reconnect with hub.
 
 
2. One more thing i want to show all users details from database in list of users (if connected than in connected user list and if not connected than in disconnected  users list) Just take a look on above screenshot i have 10 users in this group but it is showing only the details of user after they are connected with hub.
 
Take a look on my code:-
 
public void Connect(string name, string loginid, string rolename, string empcode, string supempcode, string supempcodes, string processname, string usersessionid, string grp)
//Connect(string userName)
{
var id = Context.ConnectionId;
var roomName = grp;
if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)
{
ConnectedUsers.Add(new UserDetail { ConnectionId = id, UserName = name });
// send to caller
Clients.Caller.onConnected(id, name, ConnectedUsers, CurrentMessage);
// send to all except caller client
Clients.AllExcept(id).onNewUserConnected(id, name);
//----------- Groups.Add(Context.ConnectionId, roomName);
}
public override System.Threading.Tasks.Task OnDisconnected()
{
var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
if (item != null)
{
ConnectedUsers.Remove(item);
var id = Context.ConnectionId;
Clients.All.onUserDisconnected(id, item.UserName);
}
return base.OnDisconnected();
}
// Calls when user successfully logged in
chatHub.client.onConnected = function (id, userName, userLoginId, allUsers, messages, rolename, empcode, supempcode, supempcodes, processname, tickermsg) {
setScreen(true);
$('#hdId').val(id);
$('#hdUserName').val(userName);
$('#hdUserId').val(userLoginId);
$('#hdRoleName').val(rolename);
$('#hdEmpCode').val(empcode);
$('#hdSupEmpCode').val(supempcode);
$('#hdProcessName').val(processname);
$('#spanUser').html(userName);
// Add All Users
/*
for (i = 0; i < allUsers.length; i++) {
if ($("#img_" + allUsers[i].EmployeeCode) != null) {
$("#img_" + allUsers[i].EmployeeCode).attr('src', 'Images/Online.png');
}
}*/
for (i = 0; i < allUsers.length; i++) {
AddUser(chatHub, allUsers[i].ConnectionId, allUsers[i].UserName);
}
// Add Existing Messages
for (i = 0; i < messages.length; i++) {
AddMessage(messages[i].UserName, messages[i].Message);
}
}
function IsInTeam(allUsers, empCode, supCode, UserName, Message, MsgDate, IsNewMsg, IsGroupMsg, DocIds) {
for (i = 0; i < allUsers.length; i++) {
if (empCode == allUsers[i].EmployeeCode) {
if (supCode == allUsers[i].SupEmployeeCode) {
AddMessage(UserName, Message, MsgDate, IsNewMsg, IsGroupMsg, DocIds);
return true;
}
else
{
return IsInTeam(allUsers, allUsers[i].SupEmployeeCode, supCode, UserName, Message, MsgDate, IsNewMsg, IsGroupMsg, DocIds)
}
}
}
return false;
}
chatHub.client.alreadyConnected = function (name)
{
alert(name + " is already logged in.");
}
//On New User Connected
chatHub.client.onNewUserConnected = function (id, name, rolename, empcode)
{
if ($("#" + empcode) != null)
{
$("#" + empcode).attr('src', 'Images/online.jpg');
}
}
chatHub.client.onNewUserConnected = function (id, name, rolename, empcode)
{
AddUser(chatHub, id, name);
if ($("#" + empcode) != null)
{
$("#" + empcode).attr('src', 'Images/offline.jpg');
}
}
// On User Disconnected
chatHub.client.onUserDisconnected = function (id, userName, rolename, empcode) {
if ($("#" + empcode) != null) {
$('#' + id).remove();
var ctrId = 'private_' + id;
//$('#' + ctrId).remove();
code12 = $("#" + empcode).attr('src', 'Images/offline.jpg');
var disc = $('<div class="abc"><img src="Images/offline.JPG" width="10px" />' + userName + '</div>');
$(disc).show();
$('#divusers').prepend(disc);
// $('#divusers').append(disc);
// $(disc).fadeIn(200).delay(2000).fadeOut(200);
}
else
{
$('#' + id).remove();
var ctrId = 'private_' + id;
$('#' + ctrId).remove();
code12 = $("#" + empcode).attr('src', 'Images/offline.jpg');
var disc = $('<div class="abc"><img src="Images/offline.JPG" width="10px" />' + userName + '</div>');
$(disc).remove();
// $('#divusers').prepend(disc);
$('#divusers').append(disc);
}
}
function AddUser(chatHub, id, name, empCode, supempcodes) {
var userId = $('#hdId').val();
var sup_id = $('#txtSupEmpCode').val();
var code = "";
if (userId == id)
{
//if (sup_id == supempcodes)
//{
// code1 = $('<a id="' + id + '" class="user" ><img src="Images/online.JPG" width="10px"/> <input type="checkbox" name=' + name + ' value=' + id + '> ' + name + ' ' + id + '<a>');
//}
code = $('<div class="loginUser"><img src="Images/online.JPG" width="10px"/> <input type="checkbox" value=' + id + '></div>');
//$('#' + ctrId).remove();// code = $('<div class="loginUser">' + name + "</div>");
}
else
{
// code = $('<a id="' + id + '" class="user" >' + name + '<a>');
code = $('<a id="' + id + '" class="user" ><img src="Images/online.JPG" width="10px"/> <input type="checkbox" name=' + name + ' value=' + id + '> ' + name + '<a>');
$(code).dblclick(function ()
{
var id = $(this).attr('id');
//if (userId != id)
// OpenPrivateChatWindow(chatHub, id, name);
});
}
// $("#divusers").append(code);
$("#divusers").append(code);
// $("#listofall").append(code1);
}
 
Please help me.
 
Thanks.

Answers (1)