Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by bobbymcsteels · Jun 05, 2016 at 02:02 PM · c#uiwwwjsonlitjson

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bobbymcsteels · Jun 09, 2016 at 08:56 AM

Not sure if this works but ##Bump##

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

179 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Nested Dictionary to JSON by LitJson 0 Answers

[WEBGL] Post json - complicated structure 0 Answers

Loading an 'UI skin pack' 1 Answer

How to read a nested Array Json File in unity 3d? 0 Answers

WebClient Errors when using WebClient. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges