Exemple #1
0
 private void AcceptClient()
 {
     UserName = MsgEncoding.Decode(srReceiver.ReadLine()).Body;
     if (!ValidateUserName())
     {
         return;
     }
     SendToClient(MsgCode.ConnectionSuccess, $"{IpAddress}:{Port}");
     ChatServer.IncludeUser(this);
     KeepListening();
 }
Exemple #2
0
        private void KeepListening()
        {
            try
            {
                while (true)
                {
                    strAnswer = srReceiver.ReadLine();
                    Console.WriteLine("Received " + strAnswer);
                    if (strAnswer == null)
                    {
                        ChatServer.RemoveUser(this);
                        break;
                    }
                    var answer = MsgEncoding.Decode(strAnswer);
                    switch (answer.Code)
                    {
                    case MsgCode.GlobalChat:
                        var message = answer.Body;
                        ChatServer.SendMessage(UserName, message);
                        break;

                    case MsgCode.RequestConnection:
                        ChatServer.GiveConnectionInfo(answer.Body);
                        break;

                    case MsgCode.OnlineListRequest:
                        var r = "";
                        foreach (var item in ChatServer.Connections)
                        {
                            r += item.UserName + ":";
                        }
                        SendToClient(MsgCode.OnlineListRequest, r);
                        break;
                    }
                }
            }
            catch (Exception)
            {
                ChatServer.RemoveUser(this);
            }
        }