完成
This commit is contained in:
175
Assets/Script/MainPanel/MeetingChatController.cs
Normal file
175
Assets/Script/MainPanel/MeetingChatController.cs
Normal file
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json;
|
||||
using RenderStreaming;
|
||||
using Script.Util;
|
||||
using Stary.Evo;
|
||||
using Unity.RenderStreaming;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Script
|
||||
{
|
||||
public class MeetingChatController : IController
|
||||
{
|
||||
public GameObject PanelGo;
|
||||
private MainPanel _mainPanel;
|
||||
|
||||
private Transform _content;
|
||||
private GameObjectPool _objectRightPool;
|
||||
private GameObjectPool _objectLeftPool;
|
||||
|
||||
|
||||
private Text _connectionTimeText;
|
||||
private Text _userCountText;
|
||||
|
||||
public Action<int> OnUserCountChangedEvent;
|
||||
|
||||
private List<ChatData> _chatDatas = new();
|
||||
|
||||
private InputField _messageInput;
|
||||
private Button _sendButton;
|
||||
|
||||
public void Initialize(GameObject panelGo, MainPanel mainPanel)
|
||||
{
|
||||
PanelGo = panelGo;
|
||||
_mainPanel = mainPanel;
|
||||
_content = panelGo.transform.Find("MeetingGrid/Viewport/Content");
|
||||
_objectLeftPool = panelGo.transform.Find("MeetingGrid/LeftPool").GetComponent<GameObjectPool>();
|
||||
_objectRightPool = panelGo.transform.Find("MeetingGrid/RightPool").GetComponent<GameObjectPool>();
|
||||
_connectionTimeText = panelGo.transform.Find("MeetingNum/Time").GetComponent<Text>();
|
||||
_userCountText = panelGo.transform.Find("MeetingNum/Num").GetComponent<Text>();
|
||||
|
||||
_messageInput = panelGo.transform.Find("MettingSend/InputField").GetComponent<InputField>();
|
||||
_sendButton = panelGo.transform.Find("MettingSend/SendBtn").GetComponent<Button>();
|
||||
_sendButton.onClick.AddListener(OnSendButtonClick);
|
||||
OnUserCountChangedEvent += OnUserCountChanged;
|
||||
GameObject.FindObjectOfType<MessageChannel>().OnChatMessageReceived += OnChatMessageReceivedEvent;
|
||||
}
|
||||
|
||||
|
||||
public async void OnEnter()
|
||||
{
|
||||
_connectionTimeText.text = this.GetSystem<IGlobalConfigSystem>().GetConnectionStartTime();
|
||||
_userCountText.text = this.GetSystem<IGlobalConfigSystem>().GetUserCount().ToString();
|
||||
}
|
||||
|
||||
public async void OnDestroy()
|
||||
{
|
||||
OnUserCountChangedEvent -= OnUserCountChanged;
|
||||
GameObject.FindObjectOfType<MessageChannel>().OnChatMessageReceived -= OnChatMessageReceivedEvent;
|
||||
_objectRightPool.AllRelease();
|
||||
_objectLeftPool.AllRelease();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送聊天信息
|
||||
/// </summary>
|
||||
private void OnSendButtonClick()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_messageInput.text)) return;
|
||||
var chatData = new ChatData
|
||||
{
|
||||
id = "msg_" + Guid.NewGuid().ToString(),
|
||||
senderId = this.GetSystem<IGlobalConfigSystem>().GetUserId(),
|
||||
senderName = this.GetSystem<IGlobalConfigSystem>().GetConnectionName(),
|
||||
senderAvatar = $"{this.GetSystem<IGlobalConfigSystem>().IP}/images/head/" +
|
||||
$"{this.GetSystem<IGlobalConfigSystem>().GetConnectionTexture()}.png",
|
||||
type = "text",
|
||||
content = _messageInput.text,
|
||||
isSelf = false,
|
||||
timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
||||
};
|
||||
var message = new
|
||||
{
|
||||
type = "chat-message",
|
||||
data = chatData
|
||||
};
|
||||
var data = new
|
||||
{
|
||||
message = message,
|
||||
connectionId = this.GetSystem<IGlobalConfigSystem>().GetConnectionId()
|
||||
};
|
||||
var send = new
|
||||
{
|
||||
form = this.GetSystem<IGlobalConfigSystem>().GetConnectionId(),
|
||||
to = "",
|
||||
type = "on-message",
|
||||
data = data
|
||||
};
|
||||
SignalingMessageHelper.SendMessage(JsonConvert.SerializeObject(send));
|
||||
_messageInput.text = "";
|
||||
chatData.isSelf = !chatData.isSelf;
|
||||
var utc = DateTimeOffset.Parse(chatData.timestamp, CultureInfo.InvariantCulture);
|
||||
var local = utc.ToLocalTime().ToString("HH:mm");
|
||||
chatData.timestamp = local;
|
||||
_chatDatas.Add(chatData);
|
||||
var entry = _objectRightPool.Get();
|
||||
SetMessageEntry(chatData, entry);
|
||||
}
|
||||
|
||||
private void OnChatMessageReceivedEvent(string connectionId, ChatData data)
|
||||
{
|
||||
if (connectionId != this.GetSystem<IGlobalConfigSystem>().GetConnectionId()) return;
|
||||
_chatDatas.Add(data);
|
||||
if (data.isSelf == false)
|
||||
{
|
||||
var entry = _objectLeftPool.Get();
|
||||
SetMessageEntry(data, entry);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetMessageEntry(ChatData data, GameObject entry)
|
||||
{
|
||||
var head = entry.transform.Find("head/image");
|
||||
var name = entry.transform.Find("name");
|
||||
var message = entry.transform.Find("message");
|
||||
var sprite = entry.transform.Find("sprite");
|
||||
var time = entry.transform.Find("message/messagebg/time");
|
||||
if (string.IsNullOrEmpty(data.senderAvatar))
|
||||
{
|
||||
var randomColor = WebRTCUtil.GetRandomColor();
|
||||
head.GetComponent<Image>().color = randomColor;
|
||||
head.transform.Find("Name").GetComponent<Text>().text = data.senderName.Substring(0, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
WebRTCUtil.DownloadAndSetAvatar(data.senderAvatar, head.GetComponent<Image>());
|
||||
}
|
||||
|
||||
name.GetComponent<Text>().text = data.senderName;
|
||||
|
||||
if (data.type == "text")
|
||||
{
|
||||
message.GetComponent<Text>().text = data.content;
|
||||
sprite.gameObject.SetActive(false);
|
||||
}
|
||||
else if (data.type == "file")
|
||||
{
|
||||
message.gameObject.SetActive(false);
|
||||
sprite.gameObject.SetActive(true);
|
||||
//TODO 图片传输功能
|
||||
if (WebRTCUtil.TryDecodeDataUrlToTexture(data.content, out var texture))
|
||||
sprite.GetComponent<Image>().sprite = Sprite.Create(texture,
|
||||
new Rect(0, 0, texture.width, texture.height),
|
||||
Vector2.one);
|
||||
}
|
||||
|
||||
var utc = DateTimeOffset.Parse(data.timestamp, CultureInfo.InvariantCulture);
|
||||
var local = utc.ToLocalTime().ToString("HH:mm");
|
||||
data.timestamp = local;
|
||||
time.GetComponent<Text>().text = data.timestamp;
|
||||
}
|
||||
|
||||
private void OnUserCountChanged(int obj)
|
||||
{
|
||||
_userCountText.text = obj.ToString();
|
||||
}
|
||||
|
||||
public IArchitecture GetArchitecture()
|
||||
{
|
||||
return MainArchitecture.Interface;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user