Ejemplo n.º 1
0
        /// <summary>
        /// 离开群组
        /// </summary>
        /// <param name="groupName"></param>
        /// <returns></returns>
        public async Task LeaveGroup(string groupName)
        {
            var from = UserListHandler.GetInstance().Where(a => a.Key == Context.ConnectionId).ToList()[0];

            //从群列表中移除该成员,如果该群中人数人为0,则移除该群
            bool result = GroupListHandler.RemoveConnectedId(Context.ConnectionId, groupName);

            ////提醒有人退出群聊
            await Clients.Group(groupName).SendAsync("SendGroup", new Message
            {
                type = MsgType.GroupUserLeave,
                data = new { key = Context.ConnectionId, value = from.Value, group = groupName }
            });

            //await Clients.Caller.SendAsync("SendGroup", new Message
            //{
            //    type = MsgType.GroupUserLeave,
            //    data = new { key = Context.ConnectionId, value = from.Value, group = groupName }
            //});
            if (result)
            {
                await Clients.All.SendAsync("GroupList", new Message
                {
                    type = MsgType.GroupRemove,
                    data = groupName
                });
            }
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 断开时
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public override async Task OnDisconnectedAsync(Exception ex)
        {
            var user = UserListHandler.GetInstance().Where(a => a.Key == Context.ConnectionId).ToList()[0];

            //var data = user.Select(a => new { key = a.Key, value = a.Value }).ToList()[0];

            //从群列表中移除该成员,如果该群中人数人为0,则移除该群
            GroupListHandler.RemoveConnectedId(Context.ConnectionId, out var listgroup, out var list);
            foreach (var item in list)//通知群成员已退出群
            {
                await Clients.Group(item).SendAsync(PushMsg.SendGroup, new Message
                {
                    type = MsgType.GroupUserLeave,
                    data = new { connectId = Context.ConnectionId, connectName = user.Value, group = item }
                });
            }
            foreach (var item in listgroup)//通知所有人 群已解散
            {
                await Clients.All.SendAsync(PushMsg.GroupList, new Message
                {
                    type = MsgType.GroupRemove,
                    data = item
                });
            }

            //列表下线
            await Clients.All.SendAsync(PushMsg.List, new Message
            {
                type = MsgType.RemoveUser,
                data = new { connectId = user.Key, connectName = user.Value }
            });

            //提示下线
            await Clients.All.SendAsync(PushMsg.Send, new Message
            {
                type = MsgType.RemoveInfo,
                data = new { connectId = user.Key, value = user.Value }
            });

            UserListHandler.GetInstance().Remove(Context.ConnectionId);
            await base.OnDisconnectedAsync(ex);
        }