Refresh Chat room messages
Hi
I'm currently developing a chat feature, it GETs message and message info from a JSON file on a server and displays them in a UI text. When the app starts it displays the messages perfectly, however when a new message is POSTed to the server I can't figure out how to update the displayed messages in the UI text.
Does someone have an idea how to do this or can point me in the right direction? Below is my Chat script.
Thanks
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;
}
}
Comment
Your answer
Follow this Question
Related Questions
Nested Dictionary to JSON by LitJson 0 Answers
[WEBGL] Post json - complicated structure 0 Answers
Loading an 'UI skin pack' 1 Answer