louis Tunde

louis Tunde

  • 1.7k
  • 257
  • 45.5k

Chat Application

Oct 11 2019 5:41 AM
Please I need help
I am creating a chat application in ASP.NET C#. I used the step by Altaf Ansari on Signal R Chat App with Webform and Bootstrap - Part One.
I used the code in the chatHub Class by the code display error in the following
1. Context
2. Clients
3. OnDisconnected
 
The complete code is displayed below
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using Microsoft.AspNet.SignalR;  
  6.   
  7. namespace CHAPP_WEB  
  8. {  
  9.     public class ChutHub  
  10.     {  
  11.         static List ConnectedUsers = new List();  
  12.         static List CurrentMessage = new List();  
  13.         ConnClass ConnC = new ConnClass();  
  14.          
  15.         public void Connect(string userName)  
  16.         {  
  17.             var id = Context.ConnectionId;  
  18.   
  19.             if (ConnectedUsers.Count(x => x.ConnectionId == id) == 0)  
  20.             {  
  21.                 string UserImg = GetUserImage(userName);  
  22.                 string logintime = DateTime.Now.ToString();  
  23.                 ConnectedUsers.Add(new Users { ConnectionId = id, UserName = userName, UserImage = UserImg, LoginTime = logintime });  
  24.   
  25.                 // send to caller  
  26.                 Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);  
  27.   
  28.                 // send to all except caller client  
  29.                 Clients.AllExcept(id).onNewUserConnected(id, userName, UserImg, logintime);  
  30.             }  
  31.         }  
  32.   
  33.         public void SendMessageToAll(string userName, string message, string time)  
  34.         {  
  35.             string UserImg = GetUserImage(userName);  
  36.             // store last 100 messages in cache  
  37.             AddMessageinCache(userName, message, time, UserImg);  
  38.   
  39.             // Broad cast message  
  40.             Clients.All.messageReceived(userName, message, time, UserImg);  
  41.         }  
  42.   
  43.         private void AddMessageinCache(string userName, string message, string time, string UserImg)  
  44.         {  
  45.             CurrentMessage.Add(new Messages { UserName = userName, Message = message, Time = time, UserImage = UserImg });  
  46.   
  47.             if (CurrentMessage.Count > 100)  
  48.                 CurrentMessage.RemoveAt(0);  
  49.   
  50.             // Refresh();  
  51.         }  
  52.   
  53.         public string GetUserImage(string username)  
  54.         {  
  55.             string RetimgName = "images/dummy.png";  
  56.             try  
  57.             {  
  58.                 string query = "select Photo from tbl_Users where UserName='" + username + "'";  
  59.                 string ImageName = ConnC.GetColumnVal(query, "Photo");  
  60.   
  61.                 if (ImageName != "")  
  62.                     RetimgName = "images/DP/" + ImageName;  
  63.             }  
  64.             catch (Exception ex)  
  65.             { }  
  66.             return RetimgName;  
  67.         }  
  68.   
  69.         public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)  
  70.         {  
  71.             var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);  
  72.             if (item != null)  
  73.             {  
  74.                 ConnectedUsers.Remove(item);  
  75.                 var id = Context.ConnectionId;  
  76.                 Clients.All.onUserDisconnected(id, item.UserName);  
  77.             }  
  78.             return base.OnDisconnected(stopCalled);  
  79.         }  
  80.     }  
  81. }
Please help me out.
Thanks

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (2)