Beispiel #1
0
        List <UserInstance> connectedUsers = new List <UserInstance>(); // TODO : later use DateBase instead

        protected internal void ListenConnections()
        {
            server = new TcpListener(IPAddress.Any, 8080);
            server.Start();

            Console.WriteLine("Server is active. Waiting for connections...");

            while (true)
            {
                var tcpClient = server.AcceptTcpClient();

                var user = new UserInstance(tcpClient, this);
                var userAuthorization = new UserAuthorization(user, user.toRegister);
                if (UserConnectedToServer(user.userNickName))
                {
                    AnswerToClient(user, false.ToString());
                }
                else
                {
                    if (userAuthorization.successfullyVerified)
                    {
                        ConnectUser(user);
                        var clientThread = new Thread(new ThreadStart(user.BroadcastMessageToChat));
                        clientThread.Start();
                    }
                    AnswerToClient(user, userAuthorization.successfullyVerified.ToString());
                }
            }
        }
Beispiel #2
0
 void AnswerToClient(UserInstance userToWhomAnswer, string messageToAnswer)
 {
     try
     {
         byte[] writeBuffer = Encoding.Unicode.GetBytes(messageToAnswer);
         userToWhomAnswer.dataTransferStream.Write(writeBuffer, 0, writeBuffer.Length);
     }
     catch (Exception) { Console.WriteLine("Could not answer to client."); }
 }
Beispiel #3
0
        protected internal void ConnectUser(UserInstance userToConnect)
        {
            connectedUsers.Add(userToConnect);

            string messageAboutJoining = $"{DateTime.Now.ToShortTimeString()}" +
                                         $" : {userToConnect.userNickName} connected to the server.";

            // TODO : clause for server : user 'logged'/'registered' and then connected to the server

            NotifyAllUsers(messageAboutJoining, userToConnect.userID);
            NotifyServer(messageAboutJoining, ConsoleColor.Green);
        }