//建群回复解析 public static void decodeCreateGroupAnswer(command cComand) { JArray data = (JArray)cComand.data;//转化为Jobject //ArrayList newGroupMessage = data.ToObject<ArrayList>(); group newGroup = new group(data[0].ToObject <int>(), data[1].ToObject <string>(), data[2].ToObject <List <user> >()); groupList.Add(newGroup); if (m1 != null) { m1.drawList(); } }
//群组成员变动解析 public static void decodeGroupMemberChange(command cComand) { JObject data = (JObject)cComand.data; //转化为Jobject List <int> changeMessage = data.ToObject <List <int> >(); //获取变动信息{退群成员ID,群组ID} foreach (group i in groupList) { if (i.id == changeMessage[1]) { i.deleteUser((uint)changeMessage[0]); } } }
public void SendBigFile(string filePath, int uID, List <int> targetID, string fileType) { Socket socketSend = cSockets[0]; string fileName = "null"; try { //读取选择的文件 using (FileStream fsRead = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read)) { //1. 第一步:发送一个包,表示文件的长度,让客户端知道后续要接收几个包来重新组织成一个文件 long length = fsRead.Length; fileName = filePath.Substring(filePath.LastIndexOf('\\') + 1); ArrayList fileInfo = new ArrayList() { length, uID, targetID, fileName }; command c1 = new command() { data = fileInfo, type = 1, subType = fileType, res = "yes" }; string sendStr = JsonConvert.SerializeObject(c1); sendSysMsg(sendStr, 1); //2. 第二步:每次发送一个1MB的包,如果文件较大,则会拆分为多个包 byte[] buffer = new byte[1024 * 1024]; long send = 0; //发送的字节数 while (true) //大文件断点多次传输 { int r = fsRead.Read(buffer, 0, buffer.Length); if (r == 0) { break; } socketSend.Send(buffer, 0, r, SocketFlags.None); send += r; } //删除文件 } } catch { Console.WriteLine("收发文件错误!"); } imageFileMessage message = new imageFileMessage(); message.target = (int)(publicClass.s1.targetUserID); message.sendUser = publicClass.mainUser.id; message.sendTime = DateTime.Now; message.fileName = filePath; publicClass.s1.showImageFileMessage(message); }
//登录解析 public static void decodeLogin(command cCommand) { if (cCommand.res.ToString() == "yes") { mainUser.id = Convert.ToInt32(cCommand.data);//赋值给主用户 l1.Invoke(new Action(() => { l1.DialogResult = DialogResult.OK; l1.Close(); })); } else { MessageBox.Show("登录错误,请重试!"); } }
//群聊图片文件信息解析 public static void decodeGroupIFMessageDraw(command cComand) { JObject data = (JObject)cComand.data; //转化为Jobject imageFileMessage message = data["msg"].ToObject <imageFileMessage>(); //获取消息类 foreach (group i in groupList) { if (i.id == message.target) { i.addMessage(message); } } if (g1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 1); })); } else if (g1.groupID != (uint)message.target) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 1); })); } else if (g1.groupID == (uint)message.target) { if (g1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 1); })); } else { g1.Invoke(new Action(() => { g1.AddMessage(message); })); } } }
//mainPage列表渲染解析 public static void decodeMainPageList(command cCommand) { JArray allThreeList = (JArray)(cCommand.data); //赋值本地数据 onlineUserList = allThreeList[0].ToObject <List <user> >(); groupList = allThreeList[1].ToObject <List <group> >(); recentChatList = allThreeList[2].ToObject <List <user> >(); m1.Invoke(new Action(() => { m1.drawList(); })); if (g1 != null)//如果有群聊对话框打开,也要重绘内部成员列表 { g1.Invoke(new Action(() => { g1.drawList(); })); } }
//通用解析函数分发函数 public static void decodeCommand(command cComand) { if (cComand.type == 0)//系统消息 { switch (cComand.subType) { case ("checkNameAnswer"): decodeUserNameCheck(cComand); //重名查询 break; case ("loginAnswer"): decodeLogin(cComand); //登录回应 break; case ("mainPageListDraw"): decodeMainPageList(cComand); //mainPage渲染 break; case ("groupMemberChange"): decodeGroupMemberChange(cComand); //group成员变动 break; case ("createGroupAnswer"): decodeCreateGroupAnswer(cComand); //建群回复 break; } } else if (cComand.type == 1) //普通消息 { switch (cComand.subType) { case ("singleChatTextMessageDraw"): decodeSingleTextMessageDraw(cComand); break; case ("singleChatIFMessageDraw"): decodeSingleIFMessageDraw(cComand); break; case ("groupChatTextMessageDraw"): decodeGroupTextMessageDraw(cComand); break; case ("groupChatIFMessageDraw"): decodeGroupIFMessageDraw(cComand); break; } } }
//解析函数簇 //用户名检测 public static void decodeUserNameCheck(command cCommand) { if (cCommand.data.ToString() == "yes") { l1.Invoke(new Action(() => { l1.userNameCheck(true); })); } else if (cCommand.data.ToString() == "no") { //l1.userNameCheck(false); l1.Invoke(new Action(() => { l1.userNameCheck(false); })); } else { MessageBox.Show("登录错误,请重试!"); } }
//单聊图片文件信息解析 public static void decodeSingleIFMessageDraw(command cComand) { JArray data = (JArray)cComand.data;//转化为Jobject int uID = data[1].ToObject <int>(); int targetID = data[2].ToObject <int>(); string fileName = data[3].ToString(); imageFileMessage message = new imageFileMessage(); message.fileName = fileName; try { string endfix = fileName.Substring(fileName.IndexOf('.') + 1); if (endfix == "jpg" | endfix == "jpeg" | endfix == "png" | endfix == "gif") { message.fileType = "img";//图片类型 } } catch { message.fileType = "file";//文件类型 } message.sendUser = uID; message.sendTime = DateTime.Now; if (!myChat.ContainsKey(message.sendUser)) { singleChatSession chatSession = new singleChatSession(message.sendUser); chatSession.addMessage(message); myChat.Add(message.sendUser, chatSession); if (s1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID != (uint)message.sendUser) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID == (uint)message.sendUser) { if (s1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else { s1.Invoke(new Action(() => { s1.AddMessage(message); })); } } } else { myChat[message.sendUser].addMessage(message); if (s1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID != (uint)message.sendUser) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID == (uint)message.sendUser) { if (s1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else { s1.Invoke(new Action(() => { s1.AddMessage(message); })); } } } }
//单聊文本信息解析 public static void decodeSingleTextMessageDraw(command cComand) { JObject data = (JObject)cComand.data; //转化为Jobject textMessage message = data.ToObject <textMessage>(); //获取消息类 if (!myChat.ContainsKey(message.sendUser)) { singleChatSession chatSession = new singleChatSession(message.sendUser); chatSession.addMessage(message); myChat.Add(message.sendUser, chatSession); if (s1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID != (uint)message.sendUser) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID == (uint)message.sendUser) { if (s1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else { s1.Invoke(new Action(() => { s1.AddMessage(message); })); } } } else { myChat[message.sendUser].addMessage(message); if (s1 == null) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID != (uint)message.sendUser) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else if (s1.targetUserID == (uint)message.sendUser) { if (s1.IsDisposed) { m1.Invoke(new Action(() => { m1.userHeadTwinkle((uint)message.sendUser, 0); })); } else { s1.Invoke(new Action(() => { s1.AddMessage(message); })); } } } }