Trouble understanding OnGetMessage function

omard2000
omard2000
edited April 2019 in Photon Chat
Hello,

I understand that whenever a message is received this function is called. However, I do not know how I can extract the information that is being received...
public void OnGetMessages(string channelName, string[] senders, object[] messages) {
        string msg = "";
        for (int i = 0; i < senders.Length; i++) {
            msg = string.Format("{0}{1}={2}, ", msg, senders[i], messages[i]);
        }
        Debug.LogFormat("OnGetMessages: {0} ({1}) > {2}", channelName, senders.Length, msg);
    }
This is the code that I am referring to.

For example, this is the output when I send the message "hello" from another user...
OnGetMessages: channelA (1) > 1f83f13b-666d-4f11-97d6-d4adb6624242=hello,

How can I get the sender name and the message sent from this information?

Thanks!

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    Hi @omard2000,

    The code inside OnGetMessages in the code snippet you provided is useful for logging or debugging only.

    How can I get the sender name and the message sent from this information?
    You don't have to construct a debug/log string message and then extract back the information from it.
    Just use the parameters passed to the callback method.
    The first parameter is the public channel name where the messages are received/published.
    The second and third parameters are arrays of respectively the senders' UserIDs and the actual messages.
    Usually, you can get arrays of length 1 but when you first subscribe to a channel and receive its history you may receive up to 100 messages.
    You can get the sender's UserID and the message from each index, e.g.
    index 0, first sender UserID and message:
    string senderUserId = senders[0];
    object message = messages[0];