Example #1
0
        //发送数据(不同于服务端的sendData)
        public static bool sendData(int num, List <string> data)//对发送数据进行处理,num:数据类型;data:发送的数据
        {
            if (server == null)
            {
                return(false);
            }
            string sendData = "";

            switch (num)
            {
            //登录时验证账号密码:
            //data[0]:账号    data[1]:密码
            case 0:    //格式:数据类型0$用户名$密码$
                sendData = num.ToString() + "$" + data[0] + "$" + data[1] + "$";
                break;

            //发送公共消息
            //data[0]:消息内容
            case 1:    //格式:数据类型1$sender$消息长度$消息内容$
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$";
                break;

            //私聊
            //data[0]:receiver    data[1]:消息内容
            case 2:    //格式:数据类型2$sender$receiver$消息长度$消息内容$
                sendData = num.ToString() + "$" + myChat.getUserName() +
                           "$" + data[0] + "$" + data[1] + "$" + data[2] + "$";
                break;

            //获取在线用户列表
            //data[0]:代码
            case 3:    //格式:数据类型3$代码("GETONLINE":获取在线用户列表)
                sendData = num.ToString() + "$";
                break;

            case 4:
                sendData = num.ToString() + "$" + data[0] + "$" + data[1] + "$" + data[2] + "$" + data[3] + "$";
                break;

            case 5:
                sendData = num.ToString() + "$" + data[0] + "$";
                break;

            case 6:
                myChat.addText("公共聊天室", data[1] + "已登录", null);
                myChat.addListBox(data[1]);
                break;

            case 7:
                sendData = num.ToString() + "$" + data[0] + "$";
                break;

            case 8:
                sendData = num.ToString() + "$" + data[0] + "$";
                break;

            case 10:
                sendData = num.ToString() + "$" + data[0] + "$";
                break;

            case 9:
                sendData = num.ToString() + "$" + data[0] + "$" + data[1] + "$";
                break;

            case 13:
                sendData = num.ToString() + "$" + data[0] + "$";
                break;

            case 14:
                sendData = num.ToString() + "$" + data[1] + "$";
                break;

            case 15:    //格式:数据类型21$代码:获取在线用户列表
                sendData = num.ToString() + "$" + data[0] + "$";
                break;

            //发送视频聊天请求
            case 16:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$";
                break;

            //接受视频
            case 17:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$";
                break;

            //拒绝视频
            case 18:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$";
                break;

            //挂断视频
            case 19:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$";
                break;

            //发送给自己的挂断视频消息
            case 20:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$";
                break;

            //请求语音通话
            case 21:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$";
                break;

            //接受语音
            case 22:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$";
                break;

            //拒绝语音
            case 23:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$";
                break;

            //发送给自己的挂断语音信息
            case 24:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$" + data[2] + "$";
                break;

            //发送给别人的挂断语音信息
            case 25:
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0] + "$" + data[1] + "$" + data[2] + "$";
                break;

            case 26:
                sendData = num.ToString() + "$";
                break;

            //发送文件
            case 27:
                using (FileStream fsRead = new FileStream(data[1], FileMode.Open))
                {
                    //在发送文件前先给好友发送文件的名字+扩展名,方便后面的保存操作
                    string fileName      = Path.GetFileName(data[1]);
                    string fileExtension = Path.GetExtension(data[1]);
                    //将文件中的数据读到arrFile数据
                    byte[] arrFile = new byte[1024 * 1024 * 10];
                    int    length  = fsRead.Read(arrFile, 0, arrFile.Length);
                    sendData = "27$" + myChat.getUserName() + "$" + data[0] + "$" + fileExtension + "$" + myChat.getUserName() + "给你发送的文件为:" + fileName + "$";
                    byte[] sendMessage = Encoding.Default.GetBytes(sendData);
                    int    size        = sendMessage.Length;
                    byte[] arrFileSend = new byte[length + size];
                    Buffer.BlockCopy(sendMessage, 0, arrFileSend, 0, size);
                    //表示发送的是文件数据
                    Buffer.BlockCopy(arrFile, 0, arrFileSend, size, length);
                    server.Send(arrFileSend);
                }
                return(true);

            //停止视频音乐
            case 28:
                sendData = num.ToString() + "$";
                break;

            case 404:
                sendData = "404$";
                break;

            default:
                return(false);
            }
            try
            {
                server.Send(Encoding.Default.GetBytes(sendData));
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }
Example #2
0
        //发送数据(不同于服务端的sendData)
        public static bool sendData(int num, string[] data)//对发送数据进行处理,num:数据类型;data:发送的数据
        {
            if (server == null)
            {
                return(false);
            }
            string sendData = "";

            switch (num)
            {
            //登录时验证账号密码:
            //data[0]:账号    data[1]:密码
            case 0:    //格式:数据类型0$用户名$密码$
                sendData = num.ToString() + "$" +
                           data[0] + "$" + data[1] + "$";
                break;

            //发送公共消息
            //data[0]:消息内容
            case 1:    //格式:数据类型1$sender$消息长度$消息内容$
                sendData = num.ToString() + "$" + myChat.getUserName() + "$" + data[0].Length + "$" + data[0] + "$";
                break;

            //私聊
            //data[0]:receiver    data[1]:消息内容
            case 2:    //格式:数据类型2$sender$receiver$消息长度$消息内容$
                sendData = num.ToString() + "$" + myChat.getUserName() +
                           "$" + data[0] + "$" + data[1].Length.ToString() + "$" + data[1] + "$";
                break;

            //获取在线用户列表
            //data[0]:代码
            case 3:    //格式:数据类型3$代码("GETONLINE":获取在线用户列表)
                sendData = num.ToString() + "$";
                break;

            case 4:
                sendData = num.ToString() + "$" + data[0] + "$" + data[1] + "$";
                break;

            case 5:
                myChat.addText("公共聊天室", data[1] + "已登录");
                myChat.addListBox(data[1]);
                break;

            case 404:
                sendData = "404$";
                break;

            default:
                return(false);
            }
            try
            {
                server.Send(UTF8Encoding.UTF8.GetBytes(sendData));
            }
            catch (Exception)
            {
                return(false);
            }
            return(true);
        }