Пример #1
0
        /// <summary>
        /// 群组发送消息
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public Task ClientSendMsgToGroup(ChatMessageResult result)
        {
            var groupId = result.touser.id == 0 ? Config.Default_Group_Id : result.touser.id.ToString();

            result.type    = Config.Chat_Group;  //1v1
            result.msgtype = MessageType.Custom; //聊天消息,非系统消息
            result.status  = 1;
            result.msgid   = Guid.NewGuid().ToString();
            //保存到数据库
            UserBLL.AddMessage(1, result.message, result.fromuser.id, result.touser.id, result.msgid);
            //发送给客户端
            return(Clients.Group(groupId).receiveMessage(result));
        }
Пример #2
0
        /// <summary>
        /// 客户端聊天消息
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public Task ClientSendMsgToClient(ChatMessageResult result)
        {
            var groupId = MessageHelper.GetGroupName(result.fromuser.id, result.touser.id);

            result.type    = Config.Chat_One;    //1v1
            result.msgtype = MessageType.Custom; //聊天消息,非系统消息
            result.status  = 1;
            result.msgid   = Guid.NewGuid().ToString();
            //保存到数据库
            UserBLL.AddMessage(1, result.message, result.fromuser.id, int.Parse(groupId), result.msgid);
            //发送给客户端
            return(Clients.Group(groupId).receiveMessage(result));
        }
Пример #3
0
        public async Task <ChatMessageResult> CreateForEmptyRoomAsync(Guid profileId, MessageDto model)
        {
            var user = await _userRepository.GetWithId(profileId);

            var userTemp = await _userRepository.GetProfileForUserName(model.account);

            var allRoomLists = await _chatRoomListRepository.GetAllAsync();

            var roomList    = allRoomLists.Where(e => e.UserId == user.Id).ToList();
            var roomListTwo = allRoomLists.Where(e => e.UserId == userTemp.Id).ToList();

            //var uniqRoom = await _domainContext.ChatRooms.FirstOrDefaultAsync(e => e.Id == (roomList.Union));

            var dto     = new ChatMessageResult();
            var message = new ChatMessage();

            if (roomList.Count > 0 && roomListTwo.Count > 0)
            {
                var temp = roomList.FirstOrDefault(e => roomListTwo.FirstOrDefault(a => a.ChatRoomId == e.ChatRoomId) != null);
                //var temp = roomList.Intersect(roomListTwo).FirstOrDefault();
                if (temp != null)
                {
                    return(await CreateMessage(
                               model : model,
                               userTo : userTemp,
                               user : user
                               ));
                }
            }
            var room           = new ChatRoom(model.chatRoomId);
            var chatRoomListTo = new ChatRoomList();

            chatRoomListTo.ChatRoomId = room.Id;
            chatRoomListTo.UserId     = user.Id;
            var chatRoomListFrom = new ChatRoomList();

            chatRoomListFrom.ChatRoomId = room.Id;
            chatRoomListFrom.UserId     = userTemp.Id;

            await _chatRoomRepository.CreateAsync(room);

            await _chatRoomListRepository.CreateAsync(chatRoomListFrom);

            await _chatRoomListRepository.CreateAsync(chatRoomListTo);

            return(await CreateMessage(
                       model : model,
                       userTo : userTemp,
                       user : user
                       ));
        }
Пример #4
0
        /* OLD
         *      public Task SendMessage(string user, string message)
         *      {
         *              return Clients.All.SendAsync("RecievedMessage", user, message);
         *      }
         */

        public async Task Send(ChatMessageResult message, string to)
        {
            var userName = Context?.User?.Identity?.Name ?? "";

            if (userName != "")
            {
                var fromUser = message;
                fromUser.IsValid = false;
                await Clients.User(userName).SendAsync("Receive", fromUser, to);
            }
            var toUser = message;

            toUser.IsValid   = true;
            toUser.profileId = toUser.profileTo;
            await Clients.User(to).SendAsync("Receive", toUser, userName);
        }