コード例 #1
0
    public static void MantenerConvo()
    {
        Socket client = socs[socs.Count - 1];

        byte[] data = new byte[1024];
        int    recv;

        recv = client.Receive(data);
        string login = Encoding.ASCII.GetString(data, 0, recv);

        string[] receivedStrings = login.Split('#');
        if (receivedStrings[0] == "LOGIN")
        {
            if (cUsuarios.LoginUserVerification(receivedStrings[1], receivedStrings[2]))
            {
                UserProfile auxUser = new UserProfile();

                auxUser = cUsuarios.GetUserProfileByCredentials(receivedStrings[1], receivedStrings[2]);

                cUsuarios.ConnectedUserProfile(auxUser);

                string sendStringLogin = "******" + auxUser.idUser.ToString() + "#" + auxUser.name +
                                         "#" + auxUser.username + "#" + auxUser.email + "#" + auxUser.status.ToString() +
                                         "#" + auxUser.points.ToString();

                data = Encoding.ASCII.GetBytes(sendStringLogin);

                client.Send(data, data.Length, SocketFlags.None);

                client.Receive(data);

                string connectedUser = Encoding.ASCII.GetString(data, 0, recv);
                Console.WriteLine(connectedUser);

                //Enviar los Contactos del Usuario
                SendFriendsToClient(client, data, recv, auxUser);

                //Enviar las Conversaciones del Usuario
                SendConversationsToClient(client, data, recv, auxUser);

                //Enviar los participantes de conversaciones
                SendParticipantsToClient(client, data, recv, auxUser);

                //Enviar los mensajes
                SendMessagesToClient(client, data, recv, auxUser);

                Comunicacion local = new Comunicacion(auxUser, client);
                listcom.Add(local);

                /////////////////////////////
                foreach (UserProfile usu in cUsuarios.GetAllFriends(auxUser.idUser))
                {
                    Comunicacion com = listcom.Find(x => x.getUsuario().idUser == usu.idUser);
                    if (com != null)
                    {
                        com.SendMessage("SERVER$CHANGESTATE$" + auxUser.idUser + "$" + 1);
                    }
                }

                /////////////////////////

                //Recivir mensajes del Usuario
                while (true)
                {
                    data = new byte[1024];
                    recv = client.Receive(data);
                    if (recv == 0)
                    {
                        Console.WriteLine("Se ha Desconectado {0}", ((IPEndPoint)client.RemoteEndPoint).Address);

                        cUsuarios.DesconectUserProfile(auxUser);
                        break;
                    }
                    string   men   = Encoding.ASCII.GetString(data, 0, recv);
                    string[] datos = men.Split('$');

                    Console.WriteLine(men);

                    //local.SendMessage(men);
                    if (datos[0] != "SERVER")
                    {
                        string mensajeConv = datos[1].Substring(datos[1].IndexOf('-') + 1);

                        cConversaciones.AddMessagetoConv(Int32.Parse(datos[0]), local.getUsuario(), mensajeConv);
                        Conversation conv = cConversaciones.GetConversation(Int32.Parse(datos[0]));

                        foreach (UserProfile usu in cConversaciones.GetAllParticipantsProfile(conv))
                        {
                            Comunicacion com = listcom.Find(x => x.getUsuario().idUser == usu.idUser);
                            if (com != null)
                            {
                                com.SendMessage(men);
                            }
                        }
                    }
                    else
                    {
                        switch (datos[1])
                        {
                        case "ASKCHAT":
                            Comunicacion comu = listcom.Find(x => x.getUsuario().idUser == int.Parse(datos[3]));
                            if (comu != null)
                            {
                                comu.SendMessage("SERVER$ASKCHAT$" + datos[2]);
                            }
                            break;

                        case "ACCCHAT":
                            Comunicacion coma = listcom.Find(x => x.getUsuario().idUser == int.Parse(datos[3]));
                            Comunicacion comb = listcom.Find(x => x.getUsuario().idUser == int.Parse(datos[2]));
                            coma.SendMessage("SERVER$ACCCHAT$" + datos[2]);
                            comb.SendMessage("SERVER$ACCCHAT$" + datos[3]);
                            videoChats.Add(new VideoChat(coma.getUsuario(), comb.getUsuario(), listcom));

                            break;

                        case "DENCHAT":

                            break;

                        case "ADDUSER":

                            break;

                        case "ADDCONVE":
                            break;

                        case "CHANGESTATE":
                            foreach (UserProfile usu in cUsuarios.GetAllFriends(int.Parse(datos[2])))
                            {
                                Comunicacion com = listcom.Find(x => x.getUsuario().idUser == usu.idUser);
                                if (com != null)
                                {
                                    com.SendMessage("SERVER$CHANGESTATE$" + datos[2] + "$" + datos[3]);
                                }
                            }
                            break;

                        case "LEVELUP":
                            foreach (UserProfile usu in cUsuarios.GetAllFriends(int.Parse(datos[2])))
                            {
                                Comunicacion com = listcom.Find(x => x.getUsuario().idUser == usu.idUser);
                                if (com != null)
                                {
                                    com.SendMessage("SERVER$LEVELUP$" + datos[2] + "$" + datos[3]);
                                }
                            }
                            break;

                        case "BUZZ":
                            Conversation conv = cConversaciones.GetConversation(Int32.Parse(datos[2]));

                            foreach (UserProfile usu in cConversaciones.GetAllParticipantsProfile(conv))
                            {
                                Comunicacion com = listcom.Find(x => x.getUsuario().idUser == usu.idUser);
                                if (com != null)
                                {
                                    com.SendMessage("SERVER$BUZZ");
                                }
                            }
                            break;
                        }
                    }
                }

                listcom.Remove(local);
            }
            else
            {
                string res = "Incorrecto#Error";
                data = Encoding.ASCII.GetBytes(res);
                client.Send(data, data.Length, SocketFlags.None);
            }
        }
        else
        {
            if (receivedStrings[0] == "REG")
            {
                bool exists = cUsuarios.LoginUserVerification(receivedStrings[1], receivedStrings[2]);
                if (!exists)
                {
                    cUsuarios.RegisterUsuario(receivedStrings[1], receivedStrings[2], receivedStrings[3], receivedStrings[4]);
                    string res = "Cuenta registrada exitosamente. Bienvenido, " + receivedStrings[3];
                    data = Encoding.ASCII.GetBytes(res);
                    client.Send(data, data.Length, SocketFlags.None);
                }
                else
                {
                    string res = "Existe";
                    data = Encoding.ASCII.GetBytes(res);
                    client.Send(data, data.Length, SocketFlags.None);
                }
            }
        }
        socs.Remove(client);
        client.Close();
    }