Help me send chat with format JSON

Hi guys!
I implemented Photon chat yesterday, but my Boss want I have to send message to server with format JSON.
How can I send text to server with format JSON and get message from JSON with normal string.
Like send {"message":"Hello world"} and get Hello world in chat box.
Please help me. Thanks a lot!

Comments

  • skyvu145skyvu145
    edited July 3
    This is code when I covert string to object and send message to server
    
     public Message Struct{ public string message;}
    Message object =  JsonUtility.FromJson(jsonString);
    chatClient.PublishMessage( "channelA", object );
    
    But I don't know how to get only text from server via
    OnGetMessages( string channelName, string[] senders, object[] messages ){}
    (I know JsonUtility.ToJson)
    :( Thanks all
  • JohnTubeJohnTube mod
    edited July 3
    Hi @skyvu145,

    Thank you for choosing Photon!

    Define message format:
    [System.Serializable]
    public class ChatMessage 
    {
        public string message;
    }
    Send:
    public void PublishJson(string channelName, ChatMessage message) 
    {
        string jsonString = JsonUtility.ToJson(message);
        chatClient.PublishMessage(channelName, jsonString);
    }
    Parse received:
    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
        for(int i=0; i(jsonString);
    ICYMI, FYI: JsonUtility has limitations (e.g. can't (de)serialize Dictionary).
    Read more about Json Serialization in Unity here.
  • skyvu145skyvu145
    edited July 3
    JohnTube said:

    Hi @skyvu145,

    Thank you for choosing Photon!

    Define message format:

    [System.Serializable]
    public class ChatMessage 
    {
        public string message;
    }
    Send:
    public void PublishJson(string channelName, ChatMessage message) 
    {
        string jsonString = JsonUtility.ToJson(message);
        chatClient.PublishMessage(channelName, jsonString);
    }
    Parse received:
    public void OnGetMessages(string channelName, string[] senders, object[] messages)
    {
        for(int i=0; i(jsonString);
    ICYMI, FYI: JsonUtility has limitations (e.g. can't (de)serialize Dictionary).
    Read more about Json Serialization in Unity here.
    Thanks for your reply.
    But my code when I show on chatbox of chanel is:
    ChatChannel channel = null; //Show on chatbox this.chatBoxText.text = channel.ToStringMessages();
    About your guide, currently I can send object(json) and get it like text perfect.
    But how can I show it if I use channel.ToStringMessages();
    Thanks
  • Hi @skyvu145,
    ChatChannel channel = null.
    if (chatClient.TryGetChannel(channelName, out channel))
    {
  • skyvu145skyvu145
    edited July 3
    Yeah, I know, I see it into demo package. I will good if I just send normal text to server.
     public void ShowChannel(string channelName)
        {
            if (string.IsNullOrEmpty(channelName))
            {
                return;
            }
    
            ChatChannel channel = null;
            bool found = this.chatClient.TryGetChannel(channelName, out channel);
            if (!found)
            {
                Debug.Log("ShowChannel failed to find channel: " + channelName);
                return;
            }
    
            this.selectedChannelName = channelName;
            this.chatBoxText.text = channel.ToStringMessages();
            Debug.Log("ShowChannel: " + this.selectedChannelName);
        }
    But I mean, now data I send is JSON format, how I can show message on chatBox if the demo use channel.ToStringMessages(); to update text. If I use it. I will show like "message":"Hello" on the chatbox. What I need is Hello :((( Is any way to implement it? Thanks.
    (P/s: Sorry I don't know how to down the line code, I press Enter but it not work)
  • Hi @skyvu145,

    You are not forced to use channel.ToStringMessages().
    You need to implement another method yourself to construct the string to be set to the UI text.
    This is out of the scope of Photon.
Sign In or Register to comment.