Exemplo n.º 1
0
        /// <summary>
        /// Send message
        /// </summary>
        /// <param name="lblTextEnvoi">String : Text to send</param>
        /// <param name="type">message | initialization</param>
        public void EnvoiDuMessage(string lblTextEnvoi, int type)
        {
            var kMessage = new KMessage(lblTextEnvoi, type);

            try
            {
                var enc = new UTF8Encoding();
                var msg = enc.GetBytes(kMessage.ReadyToSend());

                _sck.Send(msg);
                if (kMessage.GetMessageType() == KMessage.Type.Init().ToString())
                {
                    lbxTchat.Items.Add("Me : " + tbxMessageEnvoit.Text);
                    tbxMessageEnvoit.Clear();
                }
            }
            catch
            {
                MessageBox.Show("An error occured. \r\nPlease restart Kubeah Chat" + "\r\n" + "\r\n", "An error occurred");
                Application.Exit();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// The code for the method (Recipient connected/Disconnected) is integrated directly into the message sending function.
        /// It is likely to evolve and change place.
        /// The keys are used in this case as a means of comparison.
        /// </summary>
        /// <param name="aResult">Type IAsyncResult</param>
        private void MessageReceived(IAsyncResult aResult)
        {
            try
            {
                var size = _sck.EndReceiveFrom(aResult, ref _epRemote);
                if (size > 0)
                {
                    var receivedData = new byte[1464];

                    receivedData = (byte[])aResult.AsyncState;

                    var enc      = new UTF8Encoding();
                    var kMessage = new KMessage(enc.GetString(receivedData));
                    //Comparaison chaine de caractère reçu
                    if (kMessage.GetMessageType() == KMessage.Type.Init().ToString())
                    {
                        switch (kMessage.GetMessageContent())
                        {
                        case "789ZCFZTiniwjZTUvjkas79012798":
                            FrmMain.CheckForIllegalCrossThreadCalls = false;
                            _bEtatDestinataire = false;
                            RecipientStatus(_bEtatDestinataire);
                            FrmMain.CheckForIllegalCrossThreadCalls = true;
                            break;

                        case "tuiFZCz56786casdcssdcvuivgboRTSDetre67Rz7463178":
                            if (!_bEtatDestinataire)
                            {
                                FrmMain.CheckForIllegalCrossThreadCalls = false;
                                _bEtatDestinataire = true;
                                RecipientStatus(_bEtatDestinataire);
                                FrmMain.CheckForIllegalCrossThreadCalls = true;
                            }
                            break;

                        default:
                            Console.WriteLine("Default case");
                            break;
                        }
                    }
                    else
                    {
                        FrmMain.CheckForIllegalCrossThreadCalls = false;
                        lbxTchat.Items.Add("Him :      " + kMessage.GetMessageContent());
                        FrmMain.CheckForIllegalCrossThreadCalls = true;
                        if (_bNotificationsEnable)
                        {
                            KNotification.Show(kMessage.GetMessageContent());
                        }
                    }
                }

                var buffer = new byte[1500];
                _sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref _epRemote, new AsyncCallback(MessageReceived), buffer);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
                Application.Exit();
            }
        }