コード例 #1
0
        //接受消息
        public void AddMessage(object sender, AddMessageEventArgs e)
        {
            string message = e.mess;
            string appendText;
            string appendText1;
            string id;

            appendText = "<" + e.Sender + ">" + System.DateTime.Now.ToString() + Environment.NewLine + message + Environment.NewLine;
            //int txtGetMsgLength = this.Input_text.Text.Length;
            appendText1 = "<" + e.Sender + ">" + System.DateTime.Now.ToString() + Environment.NewLine;
            if (flag_FG)
            {
                id = e.Sender;//发送者
                if (e.Sender == Current_friend.ID)
                {
                    if (message == "sticker_1" || message == "sticker_2" || message == "sticker_3")//如果是表情
                    {
                        string s = message.Substring(message.Length - 1, 1);
                        int    t = int.Parse(s);
                        //richTextBox1.ReadOnly = false;
                        richTextBox1.AppendText(appendText1);

                        SetClipboard2(images[t - 1]);


                        richTextBox1.Paste();



                        richTextBox1.AppendText(Environment.NewLine);
                        richTextBox1.ScrollToCaret();
                        //richTextBox1.ReadOnly = true;
                    }
                    else
                    {
                        richTextBox1.AppendText(appendText);
                        richTextBox1.ScrollToCaret();
                    }
                }
            }
            else
            {
                id = e.ChatName;//群聊的名字
                if (e.ChatName == Current_group.ID)
                {
                    if (message == "sticker_1" || message == "sticker_2" || message == "sticker_3")//如果是表情
                    {
                        string s = message.Substring(message.Length - 1, 1);
                        int    t = int.Parse(s);

                        richTextBox1.AppendText(appendText1);

                        SetClipboard2(images[t - 1]);

                        if (richTextBox1.CanPaste(DataFormats.GetFormat(DataFormats.Bitmap)))
                        {
                            richTextBox1.Paste();
                            //richTextBox1.AppendText(Environment.NewLine);
                        }

                        richTextBox1.AppendText(Environment.NewLine);
                        richTextBox1.ScrollToCaret();
                    }
                    else
                    {
                        this.richTextBox1.AppendText(appendText);
                        this.richTextBox1.ScrollToCaret();
                    }
                }
            }


            //加入聊天记录
            string Path = Application.StartupPath + "\\" + username + "\\" + "Contacts" + "\\";

            if (!File.Exists(Path + id + "_LTJL" + ".txt"))
            {
                File.Create(Path + id + "_LTJL" + ".txt");
            }
            StreamWriter sw = new StreamWriter(Path + id + "_LTJL" + ".txt", true);

            sw.Write(appendText);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
        }
コード例 #2
0
        private void ExtractData(Object obj)
        {
            Socket client = obj as Socket;
            AddMessageEventArgs eventArgs;

            #region Head
            byte[] cmdB = new byte[2];
            client.Receive(cmdB, 1, SocketFlags.None);
            string cmd = Encoding.UTF8.GetString(cmdB);

            byte[] accountB = new byte[10];
            client.Receive(accountB, 10, SocketFlags.None);
            string account = Encoding.UTF8.GetString(accountB);

            string group = "";
            if (cmd[0] == MsgType.GroupText || cmd[0] == MsgType.GroupFile)
            {
                byte[] groupnamel = new byte[2];//群名长度
                client.Receive(groupnamel, 1, SocketFlags.None);
                string l = Encoding.UTF8.GetString(groupnamel);
                int    L;
                int.TryParse(l, out L);
                byte[] groupB = new byte[L];
                client.Receive(groupB, L, SocketFlags.None);
                group = Encoding.UTF8.GetString(groupB);
            }
            #endregion

            string msg;

            #region Message Type
            switch (cmd[0])
            {
                #region Message
            case MsgType.Text:
                msg = GetText(client);

                eventArgs = new AddMessageEventArgs(msg, account, account);
                OnAddMessage(this, eventArgs);
                break;
                #endregion

                #region GroupMessage
            case MsgType.GroupText:
                msg = GetText(client);

                eventArgs = new AddMessageEventArgs(msg, account, group);
                OnAddMessage(this, eventArgs);
                break;
                #endregion

                #region File
            case MsgType.File:
                msg = GetFile(client);

                eventArgs = new AddMessageEventArgs(msg, account, account);
                OnAddMessage(this, eventArgs);
                break;
                #endregion

                #region GroupFile
            case MsgType.GroupFile:
                msg = GetFile(client);

                eventArgs = new AddMessageEventArgs(msg, account, group);
                OnAddMessage(this, eventArgs);
                break;

                #endregion
            default:

                break;
            }
            #endregion
        }