public void addUserChat(JSendAfterLogin jSend) { userId = jSend.User.userId;; MyUserName = jSend.User.userName; userPassword = jSend.User.userPassword; userFio = jSend.User.userFio; userBirthday = jSend.User.userBirthday.ToShortDateString().ToString(); userPhone = jSend.User.userPhone; Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { for (int i = 0; i < jSend.StartInfos.Count; i++) { TextBlock textBlock; listViewItem_1.Visibility = Visibility.Visible; string lvi = XamlWriter.Save(listViewItem_1); StringReader stringReader = new StringReader(lvi); XmlReader xmlReader = XmlReader.Create(stringReader); ListViewItem listViewItem = (ListViewItem)XamlReader.Load(xmlReader); listViewI.Add(listViewItem); textBlock = (TextBlock)listViewItem.FindName("userName"); if (jSend.StartInfos[i].groupId != null) { textBlock.Text = jSend.StartInfos[i].groupName; listViewItem.Name = "id" + jSend.StartInfos[i].groupId.ToString(); listViewItem.Tag = true; } else { textBlock.Text = jSend.StartInfos[i].userName; listViewItem.Name = "id" + jSend.StartInfos[i].chatId.ToString(); listViewItem.Tag = false; } if (jSend.StartInfos[i].content != null) { textBlock = (TextBlock)listViewItem.FindName("lastMessage"); textBlock.Text = jSend.StartInfos[i].content; } else { lastMessage.Text = ""; } listViewItem.Selected += ListViewItem_1_Selected; listView1.Items.Add(listViewItem); } listView1.Items[0] = null; })); }
private void createMainWindow(JSendAfterLogin jSend) { if (jSend.User.userPassword == passwordBox.Password.ToString()) { Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { chatWindow.Show(); Close(); })); } else { Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { loginBox.Foreground = Brushes.White; loginBox.Background = Brushes.OrangeRed; passwordBox.Foreground = Brushes.White; passwordBox.Background = Brushes.OrangeRed; loginBox.Text = "Ошибка!"; passwordBox.Clear(); })); } }
public static void ReceiveMessage() { while (true) { byte[] data = new byte[550000]; StringBuilder builder = new StringBuilder(); int bytes; try { do { try { bytes = stream.Read(data, 0, data.Length); try { builder.Append(Encoding.Unicode.GetString(data, 0, bytes)); } catch { Image image = (Bitmap)((new ImageConverter()).ConvertFrom(data)); image.Save(@"D:\photo_test.jpg"); } } catch { Disconnect(); SystemErrorConnectToServer(false); Thread.Sleep(1500); continue; } }while (stream.DataAvailable); } catch { SystemErrorConnectToServer(true); } string textReceiveMessage = builder.ToString(); if (textReceiveMessage != "") { if (textReceiveMessage.Contains(passwordToServer)) { JSendAfterLogin jSend = JsonConvert.DeserializeObject <JSendAfterLogin>(textReceiveMessage); receiveLoginEv(jSend); } else if (textReceiveMessage.Contains("group")) { textReceiveMessage = textReceiveMessage.Remove(0, 5); var jSend = JsonConvert.DeserializeObject <List <userMessages> >(textReceiveMessage); UserMessEvent(jSend, true); } else if (textReceiveMessage.Contains("chat")) { textReceiveMessage = textReceiveMessage.Remove(0, 4); var jSend = JsonConvert.DeserializeObject <List <userMessages> >(textReceiveMessage); UserMessEvent(jSend, false); } } else { SystemErrorConnectToServer(false); Thread.Sleep(1500); continue; } } }