Help me send chat with format JSON
skyvu145
✭
in Photon Chat
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!
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!
0
Comments
-
This is code when I covert string to object and send message to server
public Message Struct{ public string message;} Message object = JsonUtility.FromJson<Message>(jsonString); chatClient.PublishMessage( "channelA", object );
But I don't know how to get only text from server viaOnGetMessages( string channelName, string[] senders, object[] messages ){}
(I know JsonUtility.ToJson)
Thanks all0 -
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<messages.Length; i++) { string jsonString = messages[i] as string; if (!string.IsNullOrEmpty(jsonString)) { Message message = JsonUtility.FromJson<Message>(jsonString);
ICYMI, FYI: JsonUtility has limitations (e.g. can't (de)serialize Dictionary).
Read more about Json Serialization in Unity here.0 -
Thanks for your reply.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<messages.Length; i++) { string jsonString = messages[i] as string; if (!string.IsNullOrEmpty(jsonString)) { Message message = JsonUtility.FromJson<Message>(jsonString);
ICYMI, FYI: JsonUtility has limitations (e.g. can't (de)serialize Dictionary).
Read more about Json Serialization in Unity here.
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();
Thanks0 -
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 usechannel.ToStringMessages();
to update text. If I use it. I will show like"message":"Hello"
on the chatbox. What I need isHello
(( 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)0