Ejemplo n.º 1
0
        public async void OnClientConnected(object sender, ClientConnectedEventArgs container)
        {
            if (container.ClientName == "Server")
            {
                var SendMessageToClient = Task.Run(() =>
                                                   _server.Send(new List <Guid>()
                {
                    container.ClientId
                },
                                                                Container.GetContainer(nameof(ConnectionResponse),
                                                                                       new ConnectionResponse(ResultRequest.Failure, "Запрещенное имя")))
                                                   );
                return;
            }

            if (cachedClientName.TryGetValue(container.ClientName, out Guid clientGuid))
            {
                if (clientGuid == Guid.Empty)
                {
                    cachedClientName.TryUpdate(container.ClientName, container.ClientId, Guid.Empty);

                    var SendMessage = Task.Run(() =>
                                               _server.Send(new List <Guid> {
                        container.ClientId
                    }, Container.GetContainer(nameof(ConnectionResponse), new ConnectionResponse(ResultRequest.Ok, container.ClientName)))
                                               );
                    var SendMessageAll = Task.Run(() =>
                                                  _server.SendAll(container.ClientId, Container.GetContainer(nameof(ConnectionNotice), new ConnectionNotice(container.ClientName)))
                                                  );

                    _server.SetLoginConnection(container.ClientId, container.ClientName);
                }
                else
                {
                    var SendMessageToClient = Task.Run(() =>
                                                       _server.Send(new List <Guid>()
                    {
                        container.ClientId
                    },
                                                                    Container.GetContainer(nameof(ConnectionResponse),
                                                                                           new ConnectionResponse(ResultRequest.Failure, "Такой пользователь уже есть")))
                                                       );
                }
            }
            else
            {
                cachedClientName.TryAdd(container.ClientName, container.ClientId);

                var SendMessage = Task.Run(() =>
                                           _server.Send(new List <Guid> {
                    container.ClientId
                }, Container.GetContainer(nameof(ConnectionResponse), new ConnectionResponse(ResultRequest.Ok, container.ClientName)))
                                           );
                var SendMessageAll = Task.Run(() =>
                                              _server.SendAll(container.ClientId, Container.GetContainer(nameof(ConnectionNotice), new ConnectionNotice(container.ClientName)))
                                              );

                _server.SetLoginConnection(container.ClientId, container.ClientName);

                if (!await Task.Run(() => _data.AddNewClient(new ClientInfo {
                    NameClient = container.ClientName
                })))
                {
                    //Ошибка, не получилось записать
                }
            }
        }
Ejemplo n.º 2
0
        public async void OnRequestNumbersChats(object sender, ClientRequestedNumbersChatEventArgs container)
        {
            if (_connection.cachedClientName.TryGetValue(container.NameOfClientSender, out Guid clientGuid))
            {
                List <InfoAboutChat> AllInfoAboutChat = new List <InfoAboutChat>();

                if (_cachedClientProperies.TryGetValue(container.NameOfClientSender, out ClientProperties clientProperties))
                {
                    foreach (var numberChat in clientProperties.NumbersChat)
                    {
                        if (InfoChats.TryGetValue(numberChat, out InfoChat infoChat))
                        {
                            AllInfoAboutChat.Add(new InfoAboutChat(numberChat, infoChat.OwnerChat, infoChat.NameOfClients));
                        }
                    }
                }
                else
                {
                    _cachedClientProperies.TryAdd(container.NameOfClientSender,
                                                  new ClientProperties()
                    {
                        NumbersChat = new List <int>()
                        {
                            NumberGeneralChat
                        }
                    });
                    if (InfoChats.TryGetValue(NumberGeneralChat, out InfoChat infoChat))
                    {
                        InfoChat lastValue = infoChat;
                        infoChat.NameOfClients.Add(container.NameOfClientSender);
                        InfoChats.TryUpdate(NumberGeneralChat, infoChat, lastValue);
                    }
                    AllInfoAboutChat.Add(new InfoAboutChat(NumberGeneralChat, infoChat.OwnerChat, infoChat.NameOfClients));

                    var SendMessageAboutConnectNewClient = Task.Run
                                                           (
                        () => _server.SendAll(clientGuid,
                                              Container.GetContainer(nameof(AddClientToChatResponse),
                                                                     new AddClientToChatResponse("Server", new List <string> {
                        container.NameOfClientSender
                    }, NumberGeneralChat)))
                                                           );
                    if (!await Task.Run(() => _data.AddClientToChat(new AddClientToChat {
                        NumberChat = NumberGeneralChat,
                        NameOfClients = new List <string> {
                            container.NameOfClientSender
                        }
                    })))
                    {
                    }
                }
                var SendMessageToClient = Task.Run
                                          (
                    () => _server.Send(new List <Guid> {
                    clientGuid
                },
                                       Container.GetContainer(nameof(NumbersAccessibleChatsResponse),
                                                              new NumbersAccessibleChatsResponse(AllInfoAboutChat)))
                                          );
            }
        }