SHARE
TWEET

Untitled

a guest Jun 18th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Microsoft.AspNet.SignalR;
  6.  
  7.  
  8. namespace SignalRChat
  9. {
  10.     public class ChatHub :Hub
  11.     {
  12.         static List<Users> ConnectedUsers = new List<Users>();
  13.         static List<Messages> CurrentMessage = new List<Messages>();
  14.         ConnClass ConnC = new ConnClass();
  15.  
  16.         public void Connect(string userName)
  17.         {
  18.             var id = Context.ConnectionId;
  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.                 // send to caller
  25.                 Clients.Caller.onConnected(id, userName, ConnectedUsers, CurrentMessage);
  26.  
  27.                 // send to all except caller client
  28.                 Clients.AllExcept(id).onNewUserConnected(id, userName, UserImg, logintime);
  29.             }
  30.         }
  31.  
  32.         public void SendMessageToAll(string userName, string message, string time)
  33.         {
  34.            string UserImg = GetUserImage(userName);
  35.             // store last 100 messages in cache
  36.             AddMessageinCache(userName, message, time, UserImg);
  37.  
  38.             // Broad cast message
  39.             Clients.All.messageReceived(userName, message, time, UserImg);
  40.  
  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.         }
  51.  
  52.         // Clear Chat History
  53.         public void clearTimeout()
  54.         {
  55.             CurrentMessage.Clear();
  56.         }
  57.  
  58.         public string GetUserImage(string username)
  59.         {
  60.             string RetimgName = "images/dummy.png";
  61.             try
  62.             {
  63.                 string query = "select Photo from tbl_Users where UserName='" + username + "'";
  64.                 string ImageName = ConnC.GetColumnVal(query, "Photo");
  65.  
  66.                 if (ImageName != "")
  67.                     RetimgName = "images/DP/" + ImageName;
  68.             }
  69.             catch (Exception ex)
  70.             { }
  71.             return RetimgName;
  72.         }
  73.  
  74.         public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
  75.         {
  76.             var item = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
  77.             if (item != null)
  78.             {
  79.                 ConnectedUsers.Remove(item);
  80.  
  81.                 var id = Context.ConnectionId;
  82.                 Clients.All.onUserDisconnected(id, item.UserName);
  83.  
  84.             }
  85.             return base.OnDisconnected(stopCalled);
  86.         }
  87.  
  88.         public void SendPrivateMessage(string toUserId, string message)
  89.         {
  90.  
  91.             string fromUserId = Context.ConnectionId;
  92.  
  93.             var toUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == toUserId);
  94.             var fromUser = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == fromUserId);
  95.  
  96.             if (toUser != null && fromUser != null)
  97.             {
  98.                 string CurrentDateTime = DateTime.Now.ToString();
  99.                 string UserImg = GetUserImage(fromUser.UserName);
  100.                 // send to
  101.                 Clients.Client(toUserId).sendPrivateMessage(fromUserId, fromUser.UserName, message, UserImg, CurrentDateTime);
  102.  
  103.                 // send to caller user
  104.                 Clients.Caller.sendPrivateMessage(toUserId, fromUser.UserName, message, UserImg, CurrentDateTime);
  105.             }
  106.  
  107.         }
  108.         public void saveMessage(string userName, string message)
  109.         {
  110.             string userId = fetchUserId(userName);
  111.             string newMessageId = ConnC.getLastId();
  112.  
  113.             string saveMessageQuery = "insert into MESSAGES(Id,message,userId)Values('" + newMessageId + "','" + message + "','" + userId + "')";
  114.  
  115.             ConnC.ExecuteQuery(saveMessageQuery);
  116.         }
  117.  
  118.         public string fetchUserId(string userName)
  119.         {
  120.             string query = "select * from tbl_Users where UserName ='" + userName + "'";
  121.             return ConnC.GetColumnVal(query, "ID");
  122.         }
  123.     }
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top