Esempio n. 1
0
        private async void SendMessage(string text, bool closing = false)
        {
            try
            {
                if (!Client.Connected)
                {
                    await Client.ConnectAsync("eubfwcf.cloudapp.net", 700);
                }
                var imageUrl = Kernel.ProfilePicture;
                if (string.IsNullOrEmpty(imageUrl))
                {
                    imageUrl = "http://i.epvpimg.com/2Wrnc.png";
                }
                var entry2 = new ChatEntryJson(imageUrl, text, Kernel.CustomName);

                var json   = JsonConvert.SerializeObject(entry2);
                var buffer = Encoding.UTF8.GetBytes(json);

                var entryJson = await JsonConvert.DeserializeObjectAsync <ChatEntryJson>(json);

                var entry = new ChatEntry(entryJson.ImageUrl, entryJson.Text, entryJson.Username, true);
                await Stream.WriteAsync(buffer, 0, buffer.Length);

                await Stream.FlushAsync();

                var panel = new StackPanel {
                    MaxWidth = Kernel.UI.Width, Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Right
                };
                var block2 = new TextBlock {
                    TextWrapping = TextWrapping.Wrap, Text = "  ", VerticalAlignment = VerticalAlignment.Center
                };

                panel.Children.Add(entry.Text);
                panel.Children.Add(block2);
                panel.Children.Add(entry.Img);
                ChatBox.Children.Add(panel);
                ScrollViewer.ScrollToEnd();
            }
            catch
            {
                if (closing)
                {
                    return;
                }
                MessageBox.Show("Failed to send the message. Server did not respond", "Fail");
            }
        }
Esempio n. 2
0
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(Kernel.FacebookName) && string.IsNullOrEmpty(Kernel.TwitterUsername))
            {
                MessageBox.Show("You have to login to facebook or twitter first so we can identify you.");
                Close();
                return;
            }
            try
            {
                Client.Connect("eubfwcf.cloudapp.net", 700);
                Stream = Client.GetStream();
                SendMessage("Connected!");
                while (true)
                {
                    try
                    {
                        var data  = new byte[4096];
                        var bytes = await Stream.ReadAsync(data, 0, data.Length);

                        var json      = Encoding.UTF8.GetString(data, 0, bytes).Trim().Replace("\0", string.Empty);
                        var j         = json.Split('}')[0] + "}";
                        var entryJson = await JsonConvert.DeserializeObjectAsync <ChatEntryJson>(j);

                        if (entryJson == null)
                        {
                            continue;
                        }

                        var entry = new ChatEntry(entryJson.ImageUrl, entryJson.Text, entryJson.Username, false);

                        if (entry.UserName.Text == Kernel.CustomName)
                        {
                            continue;
                        }
                        await Dispatcher.BeginInvoke(new Action(() =>
                        {
                            SoundThingy.Stop();
                            SoundThingy.Position = TimeSpan.Zero;
                            SoundThingy.Play();
                            var panel = new StackPanel {
                                MaxWidth = Kernel.UI.Width, Orientation = Orientation.Horizontal, Background = new SolidColorBrush(Color.FromRgb(194, 224, 224))
                            };
                            var block = new TextBlock {
                                Text = " said: ", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Left, TextWrapping = TextWrapping.Wrap
                            };
                            var block2 = new TextBlock {
                                Text = "  ", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Left, TextWrapping = TextWrapping.Wrap
                            };
                            panel.Children.Add(entry.Img);
                            panel.Children.Add(block2);
                            panel.Children.Add(entry.UserName);
                            panel.Children.Add(block);
                            panel.Children.Add(entry.Text);
                            ChatBox.Children.Add(panel);
                            ScrollViewer.ScrollToEnd();
                        }), DispatcherPriority.Normal);
                    }
                    catch (Exception ex)
                    {
                        IoQueue.Add(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not connect to the chat.", "fail");
                IoQueue.Add(ex);
                Close();
            }
        }