/// <summary> /// 初始化好友UI消息名单 /// </summary> void InitializeUIMessage() { if (package == null) { return; } //所有网络好友信息 foreach (FriendInfo friend in package.friendList) { //调用委托实例消息列表 if (!LitJson.JsonMapper.ToJson(friend.chat_message).Equals("[]")) { //调用委托实例消息列表 UI_MessageControl.setFriend(friend); InsertOfflineMessage(friend); } //调用委托实例通讯录列表 UI_ContactsManager.setFriend(friend); if (!friendInfos.ContainsKey(friend.friend_id)) { friendInfos.Add(friend.friend_id, friend); } } }
/// <summary> /// 给朋友发消息 /// </summary> private void ToFriendSendMessage() { //激活聊天面板 UI_LobbyManager.lobbyManager.StartMessageActivity(); if (UI_ChatPanelManagers.GetChatPanelInstance.ui_ChatPanels.ContainsKey(friendId)) { UI_ChatPanelManagers.GetChatPanelInstance.ShowChatPanel(friendId); } else { UI_MessageControl.setFriend(UI_LobbyManager.lobbyManager.friendInfos[friendId]); } }
/// <summary> /// 将接收消息 /// 查找消息好友id /// </summary> /// <param name="pack"></param> public void ReceiveMessage(DataPackage pack) { if (pack.requestType == RequestType.chat) { // 用Loom的方法在Unity主线程中调用 Loom.QueueOnMainThread((param) => { if (UI_LobbyManager.myselfInfo.user_id.Equals(pack.userInfo.user_id)) { //判断聊天面板是否存在好友聊天面板 if (ui_ChatPanels.ContainsKey(pack.friendId)) { string msg = System.Text.Encoding.UTF8.GetString(pack.data); //创建消息 ui_ChatPanels[pack.friendId].CreateFriendMsgText(msg); //插入历史聊天记录 ChatHistoryManager.GetInstance().SaveChatHistory(UI_LobbyManager.lobbyManager.friendInfos[pack.friendId], 1, msg); } else { //判断通讯录里是否有好友 if (UI_LobbyManager.lobbyManager.friendInfos.ContainsKey(pack.friendId)) { //创建面板 UI_MessageControl.setFriend(UI_LobbyManager.lobbyManager.friendInfos[pack.friendId]); /* * 加入Dictionary,但是刷新不出来,第二次接收消息时才能刷新 */ ui_ChatPanels[pack.friendId].CreateFriendMsgText(System.Text.Encoding.UTF8.GetString(pack.data)); } } } else { Debug.LogError("不是自己好友的消息"); Debug.Log("聊天:" + LitJson.JsonMapper.ToJson(pack)); } }, null); } }
/// <summary> /// 加载历史聊天记录 /// </summary> void LoadLocalHistories() { if (ChatHistoryManager.GetInstance().LoadChatHistory() != null) { //加载本地所有好友信息 foreach (FriendInfo localFriend in ChatHistoryManager.GetInstance().LoadChatHistory()) { //调用委托实例消息列表 UI_MessageControl.setFriend(localFriend); //调用委托实例通讯录列表 UI_ContactsManager.setFriend(localFriend); if (!friendInfos.ContainsKey(localFriend.friend_id)) { friendInfos.Add(localFriend.friend_id, localFriend); } } } }