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: 
chaklasiyanikun
Roku Guru

How To Manage Response From Task Node With Slow Internet?

I access My Task node in Main Scene Like below.

sub init()
m.Response = CreateObject("roSGNode", "TaskNodeFile") m.Response.control = "RUN"
end sub

And Task node Code Like below.

sub init()
   ?"init() - Start"
   readdata = CreateObject("roUrlTransfer")

   data = "http://Domain:Port" 
   readdata.setUrl(data)

   readdata.SetMinimumTransferRate(1, 75)

   ? "data is " data
   m.port = CreateObject("roMessagePort")
   readdata.setport(m.port)
   readdata.gettostring()
   readdata.SetRequest("POST")

   request = readdata.AsyncPostFromString(data) 
   ?"request : " request
   while (true)
      msg = Wait(3000, m.port) 'Here It's Crashed. I also tried with the 0 MilliSecond. " It's Give a Execution Time Out 
      statusCode = msg.GetResponseCode() 
      if (Type(msg) = "roUrlEvent")
         statusCode = msg.GetResponseCode()
         headers = msg.GetResponseHeaders()
         if msg.getresponsecode() = 200 then
            Response = msg.getstring()
            headers = msg.getresponseheadersarray()
            ?"Response : " Response
            exit while
         else
            m.top.isError = "true"
            m.readdata.asynccancel()
            exit while
         end if
else ? "wait for data" end if end while end sub

Here the issue is: I tested this Code with 2 Roku premiere+ devices.

In 1st Device Internet Speed: 10mbps and
In 2nd Device Internet Speed: 50mbps.

I checked My API Response using the Postman tool :
With 10mbps: It takes an 1800 MilliSeconds
With 50mbps: It takes a 350 MilliSeconds

In My Case With 10 Mbps, The Application crashed with this line msg = Wait(3000, m.port). Here, I also tried with the 0 MilliSeconds. After also It's Crashed and Gives Execution Timeout. If Some Internet issues, It's no waiting. It's Directly Crashed. Does anyone Found Good solutions or How to handle this Scenario?.

0 Kudos
3 REPLIES 3
necrotek
Roku Guru

Re: How To Manage Response From Task Node With Slow Internet?

First off, your task is not set up correctly

sub init()
    m.top.functionName = "codeToRun" 'what will be called on task.control ="RUN"
end sub
function codeToRun()
    'do stuff here
end function

  as you have it the task will do stuff only when created of task.control ="init"

 

since this is in a task and should be running on a no blocking thread try 

msg = Wait(1, m.port)

 otherwise you are waiting 3000 milliseconds each loop 

0 Kudos
chaklasiyanikun
Roku Guru

Re: How To Manage Response From Task Node With Slow Internet?

I already tried which you suggest. My Task node like below : 

sub init()
?"Start - init()" m.top.functionName = "MakePostRequest" 'Roku execution skips this line in my case.
?"End - init()" end sub
function MakePostRequest() 
?"Start - MakePostRequest" 'The call doesn't come here
readdata = CreateObject("roUrlTransfer") data = "http://Domain:Port" readdata.setUrl(data) readdata.SetMinimumTransferRate(1, 75) ? "data is " data m.port = CreateObject("roMessagePort") readdata.setport(m.port) readdata.gettostring() readdata.SetRequest("POST") request = readdata.AsyncPostFromString(data) ?"request : " request while (true) msg = Wait(0, m.port) statusCode = msg.GetResponseCode() if (Type(msg) = "roUrlEvent") statusCode = msg.GetResponseCode() headers = msg.GetResponseHeaders() if msg.getresponsecode() = 200 then Response = msg.getstring() headers = msg.getresponseheadersarray() ?"Response : " Response exit while else m.top.isError = "true" m.readdata.asynccancel() exit while end if
else ? "wait for data" end if end while
?"End - MakePostRequest"
end function  

I make a separate project for this but here also result is the same.

I have to do is stand there until there is a response. Because I created multiple Task nodes and every task node Depends on Each Other. Currently, your ways are happening like => Consider I used 3 Task node. so First It's 2nd Task node Response comes. After 3rd Task node Response comes then it's Run 1st Task node Response comes. But I need to run 1st, 2nd, 3rd wise.

0 Kudos
necrotek
Roku Guru

Re: How To Manage Response From Task Node With Slow Internet?

Are you sure that your "task" xml extends task? not sure why it would skip that line. "functionName" is the field that is called as a function when you "run" a task. 

https://developer.roku.com/docs/references/scenegraph/control-nodes/task.md

since this is a task and not blocking, omit the while loop and don't do async

  urlhost = CreateObject("roUrlTransfer")
    urlhost.SetCertificatesFile("common:/certs/ca-bundle.crt")
    urlhost.AddHeader("X-Roku-Reserved-Dev-Id", "")
    urlhost.InitClientCertificates()
    urlhost.SetUrl(m.top.url)
    rsp = urlhost.GetToString()
    
    ResponseJSON = ParseJSON(rsp)

 

 

0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.