我目前正在开发聊天功能,它从服务器上的JSON文件中获取消息和消息信息,并以UI文本显示它们。当应用程序启动时,它会完美地显示消息,但是当一条新消息被POST到服务器时,我无法弄清楚如何更新UI文本中显示的消息。
有人知道如何做到这一点或者能指出我正确的方向吗?以下是我的聊天脚本。
由于
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using LitJson;
using System.Collections.Generic;
using UnityEngine.UI;
using System.Text;
using System.Linq;
public class Message
{
public string messageId { get; set; }
public string messageContent { get; set; }
public string messageUsername { get; set; }
public string messageDate { get; set; }
public string messageTime { get; set; }
}
public class ChatJSON : MonoBehaviour {
//vars for textbox, displaying chat and submit button
public InputField input;
public InputField.SubmitEvent se;
public Text chatOutputText;
//urls
private string chatGETUrl = "Server URL";
private string chatPOSTUrl = "Server URL";
//JSOn
private string chatJSONString;
private JsonData chatStringData;
public string chatOutput;
public string newText;
public List<Message> chatList = new List<Message>();
public string chatNewString;
int messageCount;
public string displayTextString = "";
void Start()
{
StartCoroutine(GETChatJSON());
//GetChatText();
}
public IEnumerator GETChatJSON()
{
WWW chatwww = new WWW(chatGETUrl);
yield return chatwww;
if (chatwww.error == null)
{
Debug.Log("Connection is good to: " + chatGETUrl);
}
else
{
Debug.Log("ERROR: " + chatwww.error);
}
chatJSONString = chatwww.text;
chatStringData = JsonMapper.ToObject(chatJSONString);
GetChatText();
}
public void GetChatText()
{
string msgID;
string msgContent;
string msgUsername;
string msgDate;
string msgTime;
for (int i = 0; i < chatStringData["messages"].Count; i++)
{
msgID = chatStringData["messages"][i]["chat_message_id"].ToString();
msgContent = chatStringData["messages"][i]["msg_content"].ToString();
msgUsername = chatStringData["messages"][i]["username"].ToString();
msgDate = chatStringData["messages"][i]["msg_date"].ToString();
msgTime = chatStringData["messages"][i]["msg_time"].ToString();
string hoursToConvert = msgTime.Substring(0, 2);
string minsToConvert = msgTime.Substring(3, 2);
int hoursToInt = int.Parse(hoursToConvert) + 1;
string newtime = hoursToInt.ToString() + ":" + minsToConvert;
string newYear = msgDate.Substring(0, 4);
string newMonth = msgDate.Substring(5, 2);
string newDay = msgDate.Substring(8, 2);
string newDate = newDay + "/" + newMonth + "/" + newYear;
//displayTextString = "User: " + msgUsername + " " + "Message: " + msgContent +
// "\n Date: " + msgDate + " - " + msgTime;
chatList.Add(new Message { messageId = msgID, messageContent = msgContent, messageUsername = msgUsername,
messageDate = newDate, messageTime = newtime });
messageCount = i;
}
DisplayMessage();
}
void DisplayMessage()
{
chatOutputText.text = "";
IEnumerable<Message> sortedMessages =
from Message in chatList
select Message;
foreach (Message msg in sortedMessages)
{
chatNewString = chatNewString + msg.messageUsername + ": " + msg.messageContent + "\n"
+ msg.messageTime + " - " + msg.messageDate + "\n\n";
}
chatOutputText.text = chatNewString;
}
}