예제 #1
0
파일: MainFrame.cs 프로젝트: owchart/iteam
        /// <summary>
        /// 发送全体消息
        /// </summary>
        private void SendAll()
        {
            byte[]       fileBytes   = null;
            RadioButtonA rbBarrage   = GetRadioButton("rbBarrage");
            RadioButtonA rbText      = GetRadioButton("rbText");
            RadioButtonA rbFile      = GetRadioButton("rbFile");
            RadioButtonA rbAttention = GetRadioButton("rbAttention");
            String       text        = GetTextBox("txtSend").Text;
            String       sayText     = text;

            if (rbFile.Checked)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    text      = "sendfile('" + new FileInfo(openFileDialog.FileName).Name + "');";
                    fileBytes = File.ReadAllBytes(openFileDialog.FileName);
                    sayText   = text;
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (text == null || text.Trim().Length == 0)
                {
                    MessageBox.Show("请输入你想说的内容!", "提示");
                }
            }
            if (rbBarrage.Checked)
            {
                text = "addbarrage('" + text + "');";
            }
            else if (rbText.Checked)
            {
                text = "addtext('" + text + "');";
            }
            else if (rbAttention.Checked)
            {
                text = "how('" + GetTextBox("txtUserName").Text + "喊:" + text + "');";
            }
            ChatData chatData = new ChatData();

            chatData.m_content = text;
            if (fileBytes != null)
            {
                chatData.m_body       = fileBytes;
                chatData.m_bodyLength = fileBytes.Length;
            }
            chatData.m_from = DataCenter.UserName;
            DataCenter.SendAll(chatData);
            if (rbBarrage.Checked)
            {
                CIndicator indicator = CFunctionEx.CreateIndicator("", text, this);
                indicator.Clear();
                indicator.Dispose();
            }
            TextBoxA txtReceive = GetTextBox("txtReceive");

            txtReceive.Text += "我说:\r\n" + sayText + "\r\n";
            txtReceive.Invalidate();
            if (txtReceive.VScrollBar != null && txtReceive.VScrollBar.Visible)
            {
                txtReceive.VScrollBar.ScrollToEnd();
                txtReceive.Update();
                txtReceive.Invalidate();
            }
        }
예제 #2
0
파일: MainFrame.cs 프로젝트: owchart/iteam
        /// <summary>
        /// 发送消息
        /// </summary>
        private void Send(List <GridRow> rows)
        {
            byte[]       fileBytes   = null;
            String       text        = GetTextBox("txtSend").Text;
            RadioButtonA rbBarrage   = GetRadioButton("rbBarrage");
            RadioButtonA rbText      = GetRadioButton("rbText");
            RadioButtonA rbFile      = GetRadioButton("rbFile");
            RadioButtonA rbAttention = GetRadioButton("rbAttention");
            String       sayText     = text;

            if (rbFile.Checked)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    text      = "sendfile('" + new FileInfo(openFileDialog.FileName).Name + "');";
                    fileBytes = File.ReadAllBytes(openFileDialog.FileName);
                    sayText   = text;
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (text == null || text.Trim().Length == 0)
                {
                    MessageBox.Show("请输入你想说的内容!", "提示");
                }
            }
            if (rbBarrage.Checked)
            {
                text = "addbarrage('" + text + "');";
            }
            else if (rbText.Checked)
            {
                text = "addtext('" + text + "');";
            }
            else if (rbAttention.Checked)
            {
                text = "how('" + GetTextBox("txtUserName").Text + "喊:" + text + "');";
            }
            int  rowsSize = rows.Count;
            bool sendAll  = false;

            if (rowsSize > 0)
            {
                for (int i = 0; i < rowsSize; i++)
                {
                    GridRow     thisRow     = rows[i];
                    String      ip          = thisRow.GetCell("colP1").GetString();
                    int         port        = thisRow.GetCell("colP2").GetInt();
                    String      userID      = thisRow.GetCell("colP3").GetString();
                    ChatService chatService = null;
                    String      key         = ip + ":" + CStr.ConvertIntToStr(port);
                    chatService = DataCenter.GetClientChatService(key);
                    if (chatService != null)
                    {
                        if (!chatService.Connected)
                        {
                            int socketID = OwLib.BaseService.Connect(ip, port);
                            if (socketID != -1)
                            {
                                chatService.Connected = true;
                                chatService.SocketID  = socketID;
                                chatService.Enter();
                            }
                            else
                            {
                                sendAll = true;
                            }
                        }
                    }
                    else
                    {
                        int type = thisRow.GetCell("colP5").GetInt();
                        if (type == 1)
                        {
                            continue;
                        }
                        else
                        {
                            int socketID = OwLib.BaseService.Connect(ip, port);
                            if (socketID != -1)
                            {
                                chatService            = new ChatService();
                                chatService.SocketID   = socketID;
                                chatService.ServerIP   = ip;
                                chatService.ServerPort = port;
                                chatService.ToServer   = false;
                                DataCenter.AddClientChatService(key, chatService);
                                BaseService.AddService(chatService);
                            }
                            else
                            {
                                sendAll = true;
                            }
                        }
                    }
                    ChatData chatData = new ChatData();
                    chatData.m_content = text;
                    if (fileBytes != null)
                    {
                        chatData.m_body       = fileBytes;
                        chatData.m_bodyLength = fileBytes.Length;
                    }
                    chatData.m_from = DataCenter.UserName;
                    if (sendAll)
                    {
                        chatData.m_to = userID;
                        DataCenter.SendAll(chatData);
                    }
                    else
                    {
                        chatService.Send(chatData);
                    }
                    if (rbBarrage.Checked)
                    {
                        CIndicator indicator = CFunctionEx.CreateIndicator("", text, this);
                        indicator.Clear();
                        indicator.Dispose();
                    }
                    TextBoxA txtReceive = GetTextBox("txtReceive");
                    txtReceive.Text += "我说:\r\n" + sayText + "\r\n";
                    txtReceive.Invalidate();
                    if (txtReceive.VScrollBar != null && txtReceive.VScrollBar.Visible)
                    {
                        txtReceive.VScrollBar.ScrollToEnd();
                        txtReceive.Update();
                        txtReceive.Invalidate();
                    }
                }
            }
        }