Advertisement
amirrulilham_

chathub.cs (V)

Aug 8th, 2022
1,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. using System.Threading.Tasks;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using AsyncAwaitBestPractices;
  5. using Microsoft.AspNetCore.SignalR;
  6. namespace rekatoneApi.Hubs
  7. {
  8.  
  9.     // save chat message with read / unread status. so everytime load only send the unread without the read chat message
  10.     // read and unread status will be invoked by realtime (signalR) as well.
  11.  
  12.     //TODO: chatroom, persistent connection, user connection Id, database connection & CRUD
  13.     // [Authorize]
  14.     public class ChatHub : Hub
  15.     {
  16.         private RekatoneApiContext database = new RekatoneApiContext();
  17.         private FirebaseModule _firebaseModule = new FirebaseModule();
  18.         public ChatHub()
  19.         {
  20.         }
  21.  
  22.         public override async Task OnConnectedAsync()
  23.         {
  24.             await base.OnConnectedAsync();
  25.             System.Console.WriteLine(Context.ConnectionId + " connected");
  26.         }
  27.         public override async Task OnDisconnectedAsync(Exception exception)
  28.         {
  29.             await base.OnDisconnectedAsync(exception);
  30.             System.Console.WriteLine(Context.ConnectionId + " disconnected");
  31.         }
  32.         public async Task OnOpen(int userId, int chatroomId, string? lastStoredMessageId){
  33.             var chatList = await database.Chats.Where(x => x.ChatroomId == chatroomId).OrderBy(x => x.CreatedAt).ToListAsync();
  34.             if(!String.IsNullOrEmpty(lastStoredMessageId)){
  35.                 int index = chatList.IndexOf(chatList.Where(x => x.ChatId == lastStoredMessageId).FirstOrDefault());
  36.                 chatList.RemoveRange(0, index+1);
  37.                 System.Console.WriteLine(chatList.Count());
  38.             }
  39.             if(chatList.Count() > 0){
  40.                 foreach(var chat in chatList){
  41.                     await SendOnly(chat.UserId, chat.Message, chat.ChatroomId, chat.ChatType, chat.ChatId, chat.CreatedAt.ToString());
  42.                 }
  43.             }
  44.  
  45.         }
  46.         public async Task Send(int userId, string message, int chatroomId, string chatType, string chatId, string sendTime)
  47.         {
  48.             var dtSendTime = DateTimeOffset.FromUnixTimeMilliseconds(Convert.ToInt64(sendTime)).LocalDateTime;
  49.             await Clients.All.SendAsync(chatroomId.ToString(),userId.ToString(), message, chatType, chatId, dtSendTime.ToString());  
  50.             StoreChat(userId, message, chatroomId, chatType, chatId, dtSendTime).SafeFireAndForget(onException: ex => Console.WriteLine(ex));        
  51.             System.Console.WriteLine(message);
  52.         }
  53.         public async Task SendOnly(int userId, string message, int chatroomId, String chatType, string chatId, string sendTime)
  54.         {
  55.             // chatId = Guid.NewGuid().ToString();
  56.             await Clients.Caller.SendAsync(chatroomId.ToString(),userId.ToString(), message, chatType, chatId, sendTime);    
  57.             System.Console.WriteLine(message);
  58.         }
  59.  
  60.         public async Task StoreChat(int userId, string message, int chatroomId, String chatType, string chatId, DateTime sendTime){
  61.             // store the chat in the table
  62.             Chat chatModel = new Chat();
  63.             chatModel.ChatId = chatId;
  64.             chatModel.UserId = userId;
  65.             chatModel.ChatroomId = chatroomId;
  66.             chatModel.ChatType = chatType;
  67.             chatModel.Message = message;
  68.             chatModel.CreatedAt = sendTime;
  69.             await database.Chats.AddAsync(chatModel);
  70.             await database.SaveChangesAsync();
  71.             _firebaseModule.SendNewMessage(userId, chatId, chatroomId).SafeFireAndForget(onException: ex => Console.WriteLine(ex));
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement