Пример #1
0
  private void OnNewUserConnect(User user)
 {  
      this.Invoke((Action)(() =>  
      {
            UsersList.Items.Add(user.Nickname);
      }));  
  }  
Пример #2
0
        private void ProccesServerMessage(Message message)
        {
            var messageVariables = message.MessageText.Split(';');
            if (messageVariables.Length >= 2)
            {
                var vareb = messageVariables.Select(x =>
                 {

                     var parts = x.Split('=');
                     var variable = parts[0];
                     var value = parts[1];
                     return new { Variable = variable, Value = value };
                 }).ToArray();

                //if(!isFirstmessage)
                // {
                //     clientId = Convert.ToInt32(vareb[0].Value);
                //     isFirstmessage = true;
                //     return;
                // }
                var act = vareb.FirstOrDefault(x => x.Variable.Contains("ACTION"));
                if (act != null)
                {
                    var action = (ServerActions)Enum.Parse(typeof(ServerActions), act.Value);

                    switch (action)
                    {
                        case ServerActions.ClientId:
                            var idVariable = vareb.First(x => x.Variable == "CLIENTID");
                            _clientId = Convert.ToInt32(idVariable.Value);
                            break;
                        case ServerActions.NewClientConnected:
                            var clientId = Convert.ToInt32(vareb.First(x => x.Variable == "NEWCLIENTID").Value);
                            var nickname = vareb.First(x => x.Variable == "NICKNAME").Value;
                            var user = new User { Id = clientId, Nickname = nickname };
                            Users.Add(user);
                            if (newUserConnected != null)
                                newUserConnected(user);
                            break;
                        case ServerActions.ClientDisconnected:
                            var dclientId = Convert.ToInt32(vareb.First(x => x.Variable == "NEWCLIENTID").Value);
                            var dnickname = vareb.First(x => x.Variable == "NICKNAME").Value;
                            var duser = new User { Id = dclientId, Nickname = dnickname };
                            Users.Remove(duser);
                            if (userDisconnected != null)
                                userDisconnected(duser);
                            break;
                        default:
                            break;
                    }
                }
            }
        }