예제 #1
0
 private async void User_OnMessageStatusReceivedCallback(Message message)
 {
     await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
     {
         if (message.StatusType == StatusType.Connected)
         {
             ChatField.Items.Add(new StatusMessage(message.Name, "Connected"));
         }
         else if (message.StatusType == StatusType.Disconnected)
         {
             ChatField.Items.Add(new StatusMessage(message.Name, "Disconnected"));
         }
         if (message.StatusType == StatusType.Disconnecting)
         {
             user.Close();
             client.Close();
             user = null;
             client.Dispose();
             connected = false;
             Frame.Navigate(typeof(MainPage));
         }
         else if (message.StatusType == StatusType.ErrorDisconnect)
         {
             user.Close();
             client.Close();
             user = null;
             client.Dispose();
             connected = false;
             Frame.Navigate(typeof(MainPage));
         }
     });
 }
예제 #2
0
        public ChatUI()
        {
            this.InitializeComponent();
            this.NavigationCacheMode = NavigationCacheMode.Enabled;

            Color = NColor.GenerateRandomColor();

            Window.Current.Closed += (s, e) =>
            {
                if (user != null)
                {
                    user.CreateStatus(StatusType.Disconnecting);
                }
            };

            Loaded += async(s, e) =>
            {
                if (!connected)
                {
                    client = new TcpClient();
                    await client.ConnectAsync(Address, Port);

                    NetworkStream stream   = client.GetStream();
                    Message       secure   = MessageHelpers.GetMessage(stream);
                    string        password = "";
                    if (secure.Name == "locked")
                    {
                        PasswordDialog pd = new PasswordDialog();
                        if (await pd.ShowAsync() == ContentDialogResult.Primary)
                        {
                            password = pd.Password;
                        }
                        else
                        {
                            if (Frame.CanGoBack)
                            {
                                Frame.GoBack();
                            }
                        }
                    }

                    if (secure.Content != "")
                    {
                        SslStream ssl = new SslStream(stream);
                        ssl.AuthenticateAsClient(secure.Content);
                        user = new User(Username, ssl);
                        user.Init(password);
                    }
                    else
                    {
                        user = new User(Username, stream);
                        user.Init(password);
                    }

                    user.OnMessageReceivedCallback             += User_OnMessageReceivedCallback;
                    user.OnMessageStatusReceivedCallback       += User_OnMessageStatusReceivedCallback;
                    user.OnMessageTransferReceivedCallback     += User_OnMessageTransferReceivedCallback;
                    user.OnMessageWhisperReceivedCallback      += User_OnMessageWhisperReceivedCallback;
                    user.OnMessageInitReceivedCallback         += User_OnMessageInitReceivedCallback;
                    user.OnMesssageInformationReceivedCallback += User_OnMesssageInformationReceivedCallback;
                    user.OnErrorCallback += (d) => { Debug.WriteLine(d.Message); };
                    connected             = true;
                }
            };
        }