Ejemplo n.º 1
0
        private void GetDataRegister(object obj, EventNetworkUpdate e)
        {
            dynamic get = JsonConvert.DeserializeObject(e.Data);

            if ((JsonTypes)get.JsonType == JsonTypes.RegisterData)
            {
                var data = JsonConvert.DeserializeObject <RegisterData>(e.Data);

                if (data.Name == null)
                {
                    return;
                }

                var conflict = TcpServer.Connections.Find(x => x.client.Name == data.Name);
                if (conflict == null)
                {
                    client.Name = data.Name;
                    client.Id   = Guid.NewGuid().ToString();
                    SendData(new RegisterData {
                        Id = client.Id, Code = 0
                    });

                    ConsoleExtension.PrintText(data.Name + " зарегистрирован на сервере.");
                }
                else
                {
                    SendData(new RegisterData {
                        Code = 1, Description = "пользователь с таким именем уже существует."
                    });
                    Disconnect(null);
                }
                EventUpdate -= GetDataRegister;
            }
        }
Ejemplo n.º 2
0
        private void ReceiveMessage(object obj, EventNetworkUpdate e)
        {
            dynamic get = JsonConvert.DeserializeObject(e.Data);

            if ((JsonTypes)get.JsonType == JsonTypes.Message)
            {
                var msg = JsonConvert.DeserializeObject <Message>(e.Data);

                if (msg.Id == null)
                {
                    return;
                }

                foreach (var connect in Connections)
                {
                    if (connect.client.Id == msg.Id)
                    {
                        continue;
                    }

                    connect.SendData(msg);
                }
                ConsoleExtension.PrintMessage(msg.Name, msg.Text);
            }
        }