pusher / pusher-websocket-unity

Pusher Channels Unity Client Library
MIT License
27 stars 20 forks source link

Help getting data from JSON #30

Closed Sterling-Malory-Archer closed 2 years ago

Sterling-Malory-Archer commented 2 years ago

Hello everyone,

First of all, thank you for this plugin, it helped me tremendously.

I have this JSON that is sent via pusher:

{ "event": "TakePicture", "data": "{\"email\":\"someemail@email.com\"}", "channel": "picture" }

However, I cannot seem to access the email part, it gives me a number of errors.

This is how my code looks like:

 private void PusherOnConnected(object sender)
    {
        Debug.Log("Connected");
        channel.Bind("TakePicture", (dynamic data) =>
        {
            shouldTakePhoto = true;
            Debug.Log(data.ToString());
            //Debug.Log(data.GetType());

            dynamic dObj = JsonConvert.DeserializeObject<dynamic>(data);

            string temp = dObj["data"]["email].ToString();
            Debug.Log(temp.ToString());
            emailTo = temp;
        });
    }

However, I am getting that my dObj is null and I've been stuck for a good minute on this issue.

When debugging through Visual Studio, I am getting these errors:

"Cannot access child value on Newtonsoft.Json.Linq.JObject. "Method has no body".

I would kindly appreciate any help so I can finally resolve this.

Kind regards, Sterling

benjamin-tang-pusher commented 2 years ago

Are you seeing your received email in your Debug.Log(data.ToString()); log?

Sterling-Malory-Archer commented 2 years ago

Are you seeing your received email in your Debug.Log(data.ToString()); log?

Hi Benjamin,

I managed to fix it by using this code:

 private void PusherOnConnected(object sender)
    {
        Debug.Log("Connected");
        channel.Bind("TakePicture", (dynamic data) =>
        {
            shouldTakePhoto = true;

            try
            {
                var jTest = JsonConvert.DeserializeObject<dynamic>(data.ToString());
                Debug.Log("Here's our data: " + jTest.data.ToString());
                try
                {

                    var testEmail = JsonConvert.DeserializeObject<dynamic>(data.data.ToString());
                    Debug.Log("Here's our jTest2: " + testEmail.email.ToString());

                }
                catch (Exception ex)
                {
                    Debug.Log("Houston we have a problem2: " + ex.Message.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.Log("Houston we have a problem 1: " + ex.Message.ToString());
            }
        });
    }

However, this works in the Editor, but when I build an exe and start it up and send an eventName it doesn't fire off.

Any thought why that would happen?

twomedia commented 2 years ago

If you're using IL2CPP you can't use the channel.Bind functions with dynamic types. Use one of the other overrides without dynamic types. See: https://github.com/pusher/pusher-websocket-unity#3-il2cpp-extra-steps

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you'd like this issue to stay open please leave a comment indicating how this issue is affecting you. Thank you.