예제 #1
0
        public void Dispose()
        {
            var users      = ChatLogic.GetAllUsers();
            var tcpClients = _tcpClients.Union(users.Select(x => x.TcpClient)).ToList();

            foreach (var tcpClient in tcpClients)
            {
                DisconnectTcpClient(tcpClient);
            }

            StopTcpServer();
        }
예제 #2
0
        // Асинхронное обслуживание конкретного подключения
        private void ConnectionServiceAsync(TcpClient tcpClient)
        {
            Task.Run(() =>
            {
                var user = ConnectTcpClient(tcpClient);

                if (user == null)
                {
                    DisconnectTcpClient(tcpClient);
                    return;
                }

                ChatLogic.ReceiveMessagesFromUser(user);
                DisconnectUser(user);
            });
        }
예제 #3
0
        // Обработка подключенного пользователя и отправка ему всей нужной информации
        private bool ConnectUser(UserModel user)
        {
            var ok          = string.Concat(ChatConstants.Separator, ChatConstants.Ok, ChatConstants.Separator);
            var isConnected = ChatLogic.SendMessageToUser(user, ok);

            if (isConnected)
            {
                var    users     = ChatLogic.GetAllUsers();
                string usernames = null;

                if (users.Count == 0)
                {
                    usernames = string.Concat(ChatConstants.Separator, ChatConstants.Server, ChatConstants.Separator);
                }
                else
                {
                    usernames = string.Join(ChatConstants.Separator, users.Select(x => x.Username));
                }

                isConnected = ChatLogic.SendMessageToUser(user, usernames);
            }

            if (isConnected)
            {
                var commands = string.Empty;

                foreach (ChatCommands command in Enum.GetValues(typeof(ChatCommands)))
                {
                    commands += string.Concat(ChatConstants.Separator, command.ToName());
                }

                isConnected = ChatLogic.SendMessageToUser(user, commands);
            }

            if (isConnected)
            {
                _tcpClients.Remove(user.TcpClient);
                ChatLogic.AddUser(user);
                ChatLogic.UserStateChanged(user.Username, true);
            }

            return(isConnected);
        }
예제 #4
0
 // Отключение пользователя
 private void DisconnectUser(UserModel user)
 {
     ChatLogic.RemoveUser(user);
     ChatLogic.UserStateChanged(user.Username, false);
     DisconnectTcpClient(user.TcpClient);
 }