Beispiel #1
0
        private void btnSendRequest_Click(object sender, System.EventArgs e)
        {
            int requestId = WebConnector.SendRequest(this.txtRequestContent.Text, requesterId, receiverId);

            if (requestId != 0)
            {
                MessageBox.Show("Your request was succesffully sent!", "Request sent", MessageBoxButtons.OK, MessageBoxIcon.Information);

                RequestInformation request = new RequestInformation();
                request.Id          = requestId;
                request.RequesterId = this.requesterId;
                request.Requester   = this.sender;
                request.UserId      = this.receiverId;
                request.State       = 1;
                request.RequestText = this.txtRequestContent.Text;

                string jsonRequest = JsonConvert.SerializeObject(request);

                socketHelper.SendMessage(this.username, jsonRequest, this.sender.Username, @"/request");

                // Ask parent to remove oneself from it's controls list
                this.Parent.Controls.Remove(this);
                this.Dispose();
            }
            else
            {
                MessageBox.Show("Whoops, there was an issue while sending your request. Please try again.", "Request sending failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void socketHelper_SocketDataReceived(string data)
        {
            if (socketHelper.IsHandshake)
            {
                BaseResponse <AccountInformation> response = JsonConvert.DeserializeObject <BaseResponse <AccountInformation> >(data);

                if (response != null && response.Status == 200)
                {
                    this.accountInfo = response.Data;

                    this.contacts = WebConnector.GetContactsData(accountInfo.Id);
                    this.requests = WebConnector.GetAvailableRequests(accountInfo.Id);

                    this.DisplayContactsAndRequests();

                    this.socketHelper.IsHandshake  = false;
                    this.socketHelper.IsAuthorized = true;

                    this.UpdateClientMenu(true);
                }
                else
                {
                    MessageBox.Show("An error occurred: " + response.ErrorMessage, "Error logging in", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    this.socketHelper.CloseConnection();
                    this.UpdateClientMenu(false);
                }

                return;
            }

            Message message       = Message.CreateMessageObject(data);
            bool    isCallContent = false;

            if (message == null || (message.Command.Equals("/disconnect") && message.Content.Equals("OK")))
            {
                socketHelper.CloseConnection();

                UpdateClientOnDisconnect();
                return;
            }
            else if (message.Command.Equals(@"/online"))
            {
                string username = message.Content;
                ChangeOnlineStatus(username);

                return;
            }
            else if (message.Command.Equals(@"/request"))
            {
                RequestInformation request = null;

                try
                {
                    //Console.WriteLine(message.Content);
                    request = JsonConvert.DeserializeObject <RequestInformation>(message.Content);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred while parsing request command: " + ex.Message);
                }

                if (request != null)
                {
                    this.requests.Add(request);
                    this.FillRequestsList();
                }

                return;
            }
            else if (message.Command.Equals(@"/accept"))
            {
                AccountInformation contact = null;

                try
                {
                    contact = JsonConvert.DeserializeObject <AccountInformation>(message.Content);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred while parsing accept command: " + ex.Message);
                }

                if (contact != null)
                {
                    this.contacts.ContactsList.Add(contact);
                    this.AppendContact(contact);

                    this.ChangeOnlineStatus(contact.Username);

                    socketHelper.SendMessage(contact.Username, this.accountInfo.Username, this.accountInfo.Username, @"/online");
                }

                return;
            }
            else if (message.Command.Equals(@"/offline"))
            {
                string username = message.Content;
                ChangeOnlineStatus(username);

                return;
            }
            else if (message.Command.Contains("call"))
            {
                isCallContent = true; // We set it to true to avoid dumping call ID's into the message box

                if (message.Command.Equals("/call"))
                {
                    this.callSoundPlayer.PlayLooping();

                    MessengerContainer msgWindow = GetActiveMessengerContainer(message.Sender);
                    if (msgWindow != null)
                    {
                        msgWindow.CreateCallDialog(message.Sender);
                        msgWindow.callDialog.CallStateChanged += callDialog_CallStateChanged;
                    }
                }
                else if (message.Command.Contains("/accept"))
                {
                    MessengerContainer msgWindow = GetActiveMessengerContainer(message.Sender);

                    if (msgWindow != null)
                    {
                        msgWindow.InitiatingCall = false;
                    }

                    this.socketHelper.EstablishCall(this.accountInfo.Username);
                }
                else if (message.Command.Contains("/close"))
                {
                    if (this.socketHelper.CallHelper != null)
                    {
                        this.socketHelper.CloseCall();
                    }

                    MessengerContainer msgWindow = GetActiveMessengerContainer(message.Sender);
                    if (msgWindow != null)
                    {
                        msgWindow.InitiatingCall = false;

                        msgWindow.UpdateCallState();
                        msgWindow.callDialog.CallStateChanged -= callDialog_CallStateChanged;
                    }
                }
            }

            if (message.Content != string.Empty && !isCallContent)
            {
                if (message.Sender == this.accountInfo.Username)
                {
                    this.DumpMessage(message, true);
                }
                else
                {
                    this.DumpMessage(message);

                    this.notificationSoundPlayer.Play();
                    SetContainerNotification(message.Sender);
                }
            }
        }