Ejemplo n.º 1
0
        public void GroupChatA(string groupName, string userId, string msg)
        {
            if (string.IsNullOrEmpty(groupName))
            {
                return;
            }
            //判断自己是否在群组
            GroupHelp isEisx = groupList.Where(b => b.userId == userId && b.Name == groupName).FirstOrDefault();

            if (isEisx == null)
            {
                groupList.Add(new GroupHelp()
                {
                    Name   = groupName,
                    userId = userId
                });
            }
            //根据群组名称判断是否存在群组
            var nowGroupList = groupList.Where(b => b.Name == groupName).ToList();

            foreach (var itemG in nowGroupList)
            {
                Socket socket = ListUser[itemG.userId];
                try
                {
                    socket.Send(SendMsg($"用户({userId}=>{itemG.userId}):{msg}"));
                }
                catch (Exception e)
                {
                    Debug.WriteLine("该用户已掉线:" + itemG.userId);
                    //用户已掉线就删除掉
                    ListUser.Remove(itemG.userId);
                }
            }
        }
        public void GroupChatA(string groupName, string userId, string msg)
        {
            if (string.IsNullOrEmpty(groupName))
            {
                return;
            }
            //判断自己是否在群组
            GroupHelp isEisx = groupList.Where(b => b.userId == userId && b.Name == groupName).FirstOrDefault();

            if (isEisx == null)
            {
                groupList.Add(new GroupHelp()
                {
                    Name   = groupName,
                    userId = userId
                });
            }
            //根据群组名称判断是否存在群组
            var nowGroupList = groupList.Where(b => b.Name == groupName).ToList();

            foreach (var itemG in nowGroupList)
            {
                WebSocket socket = ListUser[itemG.userId];
                try
                {
                    if (socket != null && socket.State == WebSocketState.Open)
                    {
                        ArraySegment <byte> buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes($"用户({userId}=>{itemG.userId}):{msg}"));
                        socket.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None);
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine("该用户已掉线:" + itemG.userId);
                    //用户已掉线就删除掉
                    ListUser.Remove(itemG.userId);
                }
            }
        }