Example #1
0
        private string AfterOpenChat(OpenCorrespondence openCorr)
        {
            int _id = Convert.ToInt32(this.id);

            if (openCorr.statusType == "chat")
            {
                var query_mess_c = from um in db.userMessages
                                   join pc in db.PrivateChats on um.recipientChatId equals pc.chatId
                                   where (um.recipientChatId == openCorr.idCorr)
                                   select um;
                var qm = query_mess_c.OrderBy(q => q.createAt).ToList();

                List <userMessages> userMessages = new List <userMessages>();
                userMessages = qm;

                string jsonData = "chat" + JsonConvert.SerializeObject(userMessages, Formatting.Indented);

                Console.WriteLine(jsonData);

                return(jsonData);
            }
            else
            {
                var query_mess_g = from um in db.userMessages
                                   where um.recipientGroupId == openCorr.idCorr
                                   select um;
                var qm = query_mess_g.OrderBy(q => q.createAt).ToList();
                List <userMessages> userMessages = new List <userMessages>();
                userMessages = qm;

                string jsonData = "group" + JsonConvert.SerializeObject(userMessages, Formatting.Indented);

                Console.WriteLine(jsonData);

                return(jsonData);
            }
        }
Example #2
0
        public void Process()
        {
            try
            {
                Stream = client.GetStream();
                string message;

                while (true)
                {
                    message = GetMessage();

                    var loginSchemaFrame    = NJsonSchema.JsonSchema.FromType <LoginEvent>();
                    var messageSchemaFrame  = NJsonSchema.JsonSchema.FromType <MessageEvent>();
                    var openCorrSchemaFrame = NJsonSchema.JsonSchema.FromType <OpenCorrespondence>();

                    JSchema loginSchema    = JSchema.Parse(loginSchemaFrame.ToJson().ToString());
                    JSchema messageSchema  = JSchema.Parse(messageSchemaFrame.ToJson().ToString());
                    JSchema openCorrSchema = JSchema.Parse(openCorrSchemaFrame.ToJson().ToString());



                    if (JObject.Parse(message).IsValid(loginSchema))
                    {
                        LoginEvent loginEvent = new LoginEvent();
                        loginEvent = JsonConvert.DeserializeObject <LoginEvent>(message);

                        bool chek = Authorization(loginEvent);

                        if (chek)
                        {
                            Console.WriteLine(loginEvent.login + " вошел в чат");
                            foreach (var client in server.clients)
                            {
                                Console.WriteLine(client.id + "-- idUser");
                            }
                            //   Image image = Image.FromFile(@"D:\Archive\Archive\StepChat\StepChat\Step_Chat\Server\ChatServer\Photos\1200px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg");
                            int[] ids_rec = new int[1];
                            ids_rec[0] = id;

                            server.BroadcastMessage(СompileResponseAfterLogin(), ids_rec);
                        }
                    }

                    if (JObject.Parse(message).IsValid(messageSchema))
                    {
                        MessageEvent messageEvent = JsonConvert.DeserializeObject <MessageEvent>(message);

                        MessagesEventsFunc(messageEvent);
                    }

                    if (JObject.Parse(message).IsValid(openCorrSchema))
                    {
                        OpenCorrespondence openCorrespondence = JsonConvert.DeserializeObject <OpenCorrespondence>(message);
                        int[] ids_rec = new int[1];
                        ids_rec[0] = id;
                        server.BroadcastMessage(AfterOpenChat(openCorrespondence), ids_rec);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // в случае выхода из цикла закрываем ресурсы
                server.RemoveConnection(this.id);
                Close();
            }
        }