Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
korroziea
Channel Surfer

Infinite waiting on POST request

I want to send a post request with the body to an address, but there is an infinite waiting. Where did I go wrong?

My authorization function:

Spoiler
sub loginClient(login as String, password as String)

    progressdialog = createObject("roSGNode", "ProgressDialog")
    progressdialog.title = "Loading..."
    m.top.dialog = progressdialog

    urlXfer = CreateObject("roUrlTransfer")
    urlXfer.initClientCertificates()
    urlXfer.setUrl(m.constants.uri_get_deviceID)
    urlXfer.setPort(m.port)
    urlXfer.retainBodyOnError(true)
    byteArray = CreateObject("roByteArray")
    byteArray.fromAsciiString("test@test.com:1111")
    urlXfer.addHeader("authorization", "Basic " + byteArray.ToBase64String())
    requestBody = "{" + Chr(34) + "name" + Chr(34) + ":" + Chr(34) + "Roku" + Chr(34) + "," + Chr(34) + "}"
    if urlXfer.asyncPostFromString(requestBody)

        msg = wait(100, m.port)
        if type(msg) = "roUrlEvent"

            m.deviceId = msg.getString()

        else if msg = invalid

            urlXfer.asyncCancel()

        end if

    end if

end sub

At the moment I do not pass login and password. I just specify them directly.

 

0 Kudos
4 REPLIES 4
sanity-check
Roku Guru

Re: Infinite waiting on POST request

I guess 'm.port' is defined elsewhere? If that's missing it might be the problem...

Try 'urlXfer.setMessagePort' instead of setPort.

I usually see wait() being used within a 'while true' loop but you're blowing straight through if nothing happened in 100ms so it could be related to that.

Your body JSON looks like it's malformed - hard to tell with all those Chr(34)s though! Try formatjson instead:

requestBody = formatjson({"name":"Roku"})

 

In terms of the infinite waiting more generally, one thing I do is make an roTimespan before the wait loop, then I can check it and abort the request after it's gone on too long.

 

korroziea
Channel Surfer

Re: Infinite waiting on POST request

Thank you for replying! I listened to your advices. But still doesn't work.

Result: 

Spoiler
sub loginClient(login as String, password as String)

    progressdialog = createObject("roSGNode", "ProgressDialog")
    progressdialog.title = "Loading..."
    m.top.dialog = progressdialog

    urlXfer = CreateObject("roUrlTransfer")
    urlXfer.initClientCertificates()
    urlXfer.setUrl(m.constants.uri_get_deviceID)
    urlXfer.setMessagePort(m.port)
    urlXfer.retainBodyOnError(true)
    byteArray = CreateObject("roByteArray")
    byteArray.fromAsciiString("test@test.com:1111")
    urlXfer.addHeader("authorization", "Basic " + byteArray.ToBase64String())
    requestBody = formatJson({"name":"Roku"})
    if urlXfer.asyncPostFromString(requestBody)

        timer = CreateObject("roTimespan")
        timer.mark()

        while true

            if timer.totalSeconds() < 10

                msg = wait(100, m.port)
                if type(msg) = "roUrlEvent"

                    m.deviceId = msg.getString()

                else if msg = invalid

                    urlXfer.asyncCancel()

                end if

            else

                dialog = createObject("roSGNode", "Dialog")
                dialog.title = "Error"
                dialog.optionsDialog = true
                dialog.message = "The wait was more than 10 seconds!"
                m.top.dialog = dialog

                exit while

            end if

        end while

    end if

end sub

About m.port, I defined it in init(). I also noticed that after I delete the archive from the device, my boot window appears for a second, which is declared at the beggining of the function loginClient(...)

0 Kudos
renojim
Community Streaming Expert

Re: Infinite waiting on POST request

msg has to be something or invalid!  You're not checking for anything other than a roUrlEvent.  Try adding print msg or print type(msg) after the wait or another else.  When it comes to debugging BS, print is your best friend.  I don't see how it could hang unless in your original code you're calling loginClient in a loop.  I would also check for invalid before anything else just to make sure you never try to use msg for something if it's invalid.

msg = wait(100, m.port)
print type(msg)
if msg = invalid
   print "Canceling..."
   urlXfer.asyncCancel()
else if type(msg) = "roUrlEvent"
   print "Success!"
   m.deviceId = msg.getString()
else
   stop
end if

If it never hits the print, something is seriously wrong.  I would try creating a message port to assign to urlXfer and not using m.port to see what happens.

100ms isn't much time to wait, so I wouldn't be too surprised if you hit that code every time.

P.S. - code is easier to read if you use code tags rather than spoiler tags.  Look for the ... when you're making a post and it will expand to show more options.  The code tag button looks like </>.

Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
speechles
Roku Guru

Re: Infinite waiting on POST request

sub loginClient(login as String, password as String)

This doesn't look like it is done inside a task thread. It isn't using m.top.login or m.top.password to pass fields into the task. This looks like it is done in the render thread? For the url object to be created it must be done within a task.

urlXfer = CreateObject("roUrlTransfer")

0 Kudos