/// <summary> /// 更新好友列表 /// </summary> /// <param name="flag">增加/删除</param> /// <param name="userData">新上线的用户信息</param> /// <param name="friendlist">新上线用户的好友列表</param> private void UpdateFriendlist(byte flag, UserData userData, List<UserData> friendlist) { // 更新好友列表 FriendlistHandler friendlistHandler = new FriendlistHandler(flag, userData); string sendMsg = JsonConvert.SerializeObject(friendlistHandler); byte[] arrMsg = Encoding.UTF8.GetBytes(sendMsg); byte[] sendArrMsg = new byte[arrMsg.Length + 1]; // 设置标志位,代表登录/注册 sendArrMsg[0] = UPDATE_FRIENDLIST; Buffer.BlockCopy(arrMsg, 0, sendArrMsg, 1, arrMsg.Length); foreach (UserData userdata in friendlist) { // 如果用户在上线用户中 if (dictOnlineUserO.ContainsKey(userdata.userId) == true) { // 找到Ip string userIp = dictOnlineUserO[userdata.userId]; try { // 发送消息 dictSocket[userIp].Send(sendArrMsg); } catch { } } } }
/// <summary> /// 将friendlist中的所有好友信息发给用户以更新 /// </summary> /// <param name="userIp">请求ip</param> /// <param name="friendlist">好友列表</param> private void InitFriendlist(string userIp, List<UserData> friendlist) { // 遍历用户 foreach (UserData userData in friendlist) { // 当前用户在线 if (dictOnlineUserO.ContainsKey(userData.userId)) { // 包装消息 FriendlistHandler friendlistHandler = new FriendlistHandler(ADD_ONLINE_FRIEND, userData); string sendMsg = JsonConvert.SerializeObject(friendlistHandler); byte[] arrMsg = Encoding.UTF8.GetBytes(sendMsg); byte[] sendArrMsg = new byte[arrMsg.Length + 1]; // 设置标志位,代表登录/注册 sendArrMsg[0] = UPDATE_FRIENDLIST; Buffer.BlockCopy(arrMsg, 0, sendArrMsg, 1, arrMsg.Length); dictSocket[userIp].Send(sendArrMsg); Thread.Sleep(200); } } }