Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using AsyncAwaitBestPractices;
- using Microsoft.AspNetCore.SignalR;
- namespace rekatoneApi.Hubs
- {
- // save chat message with read / unread status. so everytime load only send the unread without the read chat message
- // read and unread status will be invoked by realtime (signalR) as well.
- //TODO: chatroom, persistent connection, user connection Id, database connection & CRUD
- // [Authorize]
- public class ChatHub : Hub
- {
- private RekatoneApiContext database = new RekatoneApiContext();
- private FirebaseModule _firebaseModule = new FirebaseModule();
- public ChatHub()
- {
- }
- public override async Task OnConnectedAsync()
- {
- await base.OnConnectedAsync();
- System.Console.WriteLine(Context.ConnectionId + " connected");
- }
- public override async Task OnDisconnectedAsync(Exception exception)
- {
- await base.OnDisconnectedAsync(exception);
- System.Console.WriteLine(Context.ConnectionId + " disconnected");
- }
- public async Task OnOpen(int userId, int chatroomId, string? lastStoredMessageId){
- var chatList = await database.Chats.Where(x => x.ChatroomId == chatroomId).OrderBy(x => x.CreatedAt).ToListAsync();
- if(!String.IsNullOrEmpty(lastStoredMessageId)){
- int index = chatList.IndexOf(chatList.Where(x => x.ChatId == lastStoredMessageId).FirstOrDefault());
- chatList.RemoveRange(0, index+1);
- System.Console.WriteLine(chatList.Count());
- }
- if(chatList.Count() > 0){
- foreach(var chat in chatList){
- await SendOnly(chat.UserId, chat.Message, chat.ChatroomId, chat.ChatType, chat.ChatId, chat.CreatedAt.ToString());
- }
- }
- }
- public async Task Send(int userId, string message, int chatroomId, string chatType, string chatId, string sendTime)
- {
- var dtSendTime = DateTimeOffset.FromUnixTimeMilliseconds(Convert.ToInt64(sendTime)).LocalDateTime;
- await Clients.All.SendAsync(chatroomId.ToString(),userId.ToString(), message, chatType, chatId, dtSendTime.ToString());
- StoreChat(userId, message, chatroomId, chatType, chatId, dtSendTime).SafeFireAndForget(onException: ex => Console.WriteLine(ex));
- System.Console.WriteLine(message);
- }
- public async Task SendOnly(int userId, string message, int chatroomId, String chatType, string chatId, string sendTime)
- {
- // chatId = Guid.NewGuid().ToString();
- await Clients.Caller.SendAsync(chatroomId.ToString(),userId.ToString(), message, chatType, chatId, sendTime);
- System.Console.WriteLine(message);
- }
- public async Task StoreChat(int userId, string message, int chatroomId, String chatType, string chatId, DateTime sendTime){
- // store the chat in the table
- Chat chatModel = new Chat();
- chatModel.ChatId = chatId;
- chatModel.UserId = userId;
- chatModel.ChatroomId = chatroomId;
- chatModel.ChatType = chatType;
- chatModel.Message = message;
- chatModel.CreatedAt = sendTime;
- await database.Chats.AddAsync(chatModel);
- await database.SaveChangesAsync();
- _firebaseModule.SendNewMessage(userId, chatId, chatroomId).SafeFireAndForget(onException: ex => Console.WriteLine(ex));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement