//对接收的数据进行处理 public static string[] receiveData() { string[] data = null; if (server == null) { return(data); } byte[] receiveByte = new byte[1024 * 1024 * 10]; try { server.Receive(receiveByte); } catch (Exception) { data = null; return(data); } string receiveString = Encoding.Default.GetString(receiveByte); //拆分消息 data = receiveString.Split('$'); //选择对应消息种类进行处理 switch (data[0]) { //消息类型0:是否允许登录账号 case "0": //直接返回已分段的消息 break; case "1": //1$sender$textLength$text$ //string sender = data[1]; //int textLength = int.Parse(data[2]); //string text = receiveString.Substring(receiveString.IndexOf('$', data[0].Length + data[1].Length + 2) + 1, textLength); //string result = sender+"["+DateTime.Now.ToString()+"]\n"+text; myChat.addText("公共聊天室", data[1], data[2]); break; //私聊 case "2": //数据类型2$sender$receiver$消息长度$消息内容$ myChat.addText(data[1], data[1], data[3]); break; case "3": for (int i = 1; i < data.Length - 1; i++) { myChat.addListBox(data[i]); } break; case "5": myChat.addListBox(data[1]); break; case "6": myChat.delListBox(data[1]); break; case "7": myFriend.getUser(data[1]); break; case "8": myFriend.getUser2(data[1]); break; case "9": myFriend.row(data[1]); break; case "11": myChat.row(data[1], data[2]); break; case "12": remind.row(data[1]); break; //视频聊天请求 case "16": myChat.HandleInformation(data[1], data[3], null); break; //接受视频 case "17": myChat.HandleInformation(data[1], data[3], null); break; //拒绝视频 case "18": myChat.HandleInformation(data[1], data[3], null); break; //发送给别人的挂断视频消息 case "19": myChat.HandleInformation(null, data[3], null); break; //发送给自己的挂断视频消息 case "20": myChat.videoClose(null, data[2]); break; //请求语音通话 case "21": myChat.HandleInformation(data[1], data[3], null); break; //接受语音 case "22": myChat.HandleInformation(data[1], data[3], null); break; //拒绝语音 case "23": myChat.HandleInformation(data[1], data[3], null); break; //发送给自己的挂断语音信息 case "24": myChat.HandleInformation(data[1], data[3], data[4]); break; //挂断语音 case "25": myChat.HandleInformation(data[1], data[3], data[4]); break; case "26": myChat.soundButton(); break; //停止视频音乐 case "28": myChat.closeMusic(); break; //发送文件消息 //case "27": // myChat.addFileText(data[1], data[2]); // break; //发送文件 case "27": myChat.receiveFile(data[1], data[3], data[4], receiveByte); break; case "404": sendData(404, null); myChat.showMessageBox("您已被强制下线"); data = null; break; default: break; } return(data); }
//对接收的数据进行处理 public static string[] receiveData() { string[] data = null; if (server == null) { return(data); } byte[] receiveByte = new byte[1024]; try { server.Receive(receiveByte); } catch (Exception) { data = null; return(data); } string receiveString = UTF8Encoding.UTF8.GetString(receiveByte); //拆分消息 data = receiveString.Split('$'); //选择对应消息种类进行处理 switch (data[0]) { //消息类型0:是否允许登录账号 case "0": //直接返回已分段的消息 break; case "1": //1$sender$textLength$text$ string sender = data[1]; int textLength = int.Parse(data[2]); string text = receiveString.Substring(receiveString.IndexOf('$', data[0].Length + data[1].Length + 2) + 1, textLength); string result = sender + "[" + DateTime.Now.ToString() + "]:\n" + text; myChat.addText("公共聊天室", result); break; //私聊 case "2": //数据类型2$sender$receiver$消息长度$消息内容$ result = data[1] + "[" + DateTime.Now.ToString() + "]:\n" + receiveString.Substring(data[0].Length + data[1].Length + data[2].Length + data[3].Length + 4, int.Parse(data[3])); if (data[1] == myChat.getUserName()) { myChat.addText(data[2], result); } else { myChat.addText(data[1], result); } break; case "3": for (int i = 1; i < data.Length - 1; i++) { myChat.addListBox(data[i]); } break; case "5": myChat.addListBox(data[1]); break; case "6": myChat.delListBox(data[1]); break; case "404": sendData(404, null); myChat.showMessageBox("您已被强制下线"); data = null; break; default: break; } return(data); }