//-->>>>> ***** Receive Request From Client [ Connect ] ***** public void Connect(string userName) { var id = Context.ConnectionId; string userGroup = ""; //Manage Hub Class //if freeflag==0 ==> Busy //if freeflag==1 ==> Free //if tpflag==0 ==> User //if tpflag==1 ==> Admin if (userName.Contains("temporary")) { //now we encounter ordinary user which needs userGroup and at this step, system assigns the first of free Admin among UsersList var strg = UsersList.Where(d => d.freeflag == "1" && d.tpflag == "1").FirstOrDefault(); //var strg = (from s in UsersList where (s.tpflag == "1") && (s.freeflag == "1") select s).First(); if (strg == null) { string msg = "All Administrators are busy, please be patient and try again"; //***** Return to Client ***** Clients.Caller.NoExistAdmin(); } else { userGroup = strg.UserGroup; //Admin becomes busy so we assign zero to freeflag which is shown admin is busy strg.freeflag = "0"; //now add USER to UsersList while (UsersList.Where(d => d.UserID == userName).Any()) { var randomId = new Random().Next(1, 100); userName = "******" + randomId.ToString() + "@temporary.com"; } UsersList.Add(new UserInfo { ConnectionId = id, UserID = userName, UserName = userName, UserGroup = userGroup, freeflag = "0", tpflag = "0", }); //whether it is Admin or User now both of them has userGroup and I Join this user or admin to specific group Groups.Add(Context.ConnectionId, userGroup); Clients.Caller.onConnected(id, userName, userName, userGroup); } } else { #region local user using (var ctx = new BaseOperations()) { var userInfo = ctx.Get <ApplicationUser>(d => d.Email == userName).Include(d => d.Roles).FirstOrDefault(); try { //You can check if user or admin did not login before by below line which is an if condition //if (UsersList.Count(x => x.ConnectionId == id) == 0) //Here you check if there is no userGroup which is same DepID --> this is User otherwise this is Admin //userGroup = DepID var roles = userInfo.Roles.ToList(); if (!roles.Any(d => d.RoleId == "onlineChatAdmin")) { //now we encounter ordinary user which needs userGroup and at this step, system assigns the first of free Admin among UsersList var strg = (from s in UsersList where (s.tpflag == "1") && (s.freeflag == "1") select s).First(); userGroup = strg.UserGroup; //Admin becomes busy so we assign zero to freeflag which is shown admin is busy strg.freeflag = "0"; //now add USER to UsersList UsersList.Add(new UserInfo { ConnectionId = id, UserID = userInfo.Id, UserName = userName, UserGroup = userGroup, freeflag = "0", tpflag = "0", }); //whether it is Admin or User now both of them has userGroup and I Join this user or admin to specific group Groups.Add(Context.ConnectionId, userGroup); Clients.Caller.onConnected(id, userName, userInfo.Id, userGroup); } else { //If user has admin code so admin code is same userGroup //now add ADMIN to UsersList UsersList.Add(new UserInfo { ConnectionId = id, AdminID = userInfo.Id, UserName = userName, UserGroup = userInfo.Id, freeflag = "1", tpflag = "1" }); //whether it is Admin or User now both of them has userGroup and I Join this user or admin to specific group Groups.Add(Context.ConnectionId, userInfo.Id); Clients.Caller.onConnected(id, userName, userInfo.Id, userInfo.Id); } } catch { string msg = "All Administrators are busy, please be patient and try again"; //***** Return to Client ***** Clients.Caller.NoExistAdmin(); } } #endregion } }