0

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;
     }
 }
  • You either need to query the server (send a GET) every N seconds or make the server tell the client that new data is available, but you'd need a bidirectional connection (like WebSockets) for that. – Camilo Terevinto Jun 13 '16 at 14:10
  • Thanks for the quick reply @cFrozenDeath, I've tried sending a GET every few seconds which seems to delete the contents of the UI.Text. I've additionally tried a GET in the Update attached to a for loop to check that number of 'chatStringData["messages"].Count' has changed since the starting GET, this seems to work, however I think the issues are in how I'm adding the messages to the UI.Text. – bobbymcsteels Jun 13 '16 at 14:34
  • That's another problem altogether and I'm pretty sure the standard UI.Text wont help you – Camilo Terevinto Jun 13 '16 at 16:01

0 Answers0