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: 
haamro
Visitor

Re: Returning value from function with Task

Here is my code, did some update on getData

MyTask.xml
<component name = "haamroContent" extends = "Task" >
 
  <interface>
    <field id = "HTTPurl" type = "string" />
    <field id = "HTTPcontent" type = "string" />
  </interface>
 
  <script type = "text/brightscript" >


    <![CDATA[
 
    sub init()
      m.top.functionName = "getData"
    end sub
 
  function getData()
    url = m.top.HTTPurl
    http = createObject("roUrlTransfer")
    http.RetainBodyOnError(true)
    port = createObject("roMessagePort")
    http.setPort(port)
    http.setCertificatesFile("common:/certs/ca-bundle.crt")
    http.InitClientCertificates()
    http.enablehostverification(false)
    http.enablepeerverification(false)
    http.setUrl(url)
    if http.AsyncGetToString() Then
      msg = wait(10000, port)
      if (type(msg) = "roUrlEvent")
        'HTTP response code can be <0 for Roku-defined errors
        if (msg.getresponsecode() > 0 and  msg.getresponsecode() < 400)
          m.top.HTTPcontent = msg.getstring()
        else
          ? "feed load failed: "; msg.getfailurereason();" "; msg.getresponsecode();" "; m.top.HTTPurl
          m.top.HTTPcontent = ""
        end if
        http.asynccancel()
      else if (msg = invalid)
        ? "feed load failed."
        m.top.HTTPcontent =""
        http.asynccancel()
      end if
    end if
end function
   
    ]]>


  </script>

 
</component>



myfunction.brs
function getHTMLContent(webpage as string)

   m.haamro = createObject("RoSGNode", "haamroContent")
   m.haamro.HTTPurl=webpage
   m.haamro.functionName="getdata"
   m.haamro.ObserveField("HTTPcontent","gotContent")
   m.haamro.control = "RUN"
  
end function

sub gotContent()
    m.top.content = m.haamro.HTTPcontent   '<==================  callback triggered // content is set on Scene (myfunction.xml)  where I will be using my function
print m.top.content <--- this is working and prints the output
end sub

function GrabWOD(webpage as string)    <==== This is the function I am trying to call from other functions

    getHTMLcontent(webpage) <-- calling my getHTMLcontent function, which triggers gotContent
    html=m.top.content <-- isn't m.top.content now set ??
    
    print html <-- not working
   return html <-- not working

end function

thank you for all your help.
0 Kudos
joetesta
Roku Guru

Re: Returning value from function with Task

I think you're almost there,  But you can't expect m.top.content to be set immediately after you call the task. You need to wait for a callback and process it there.
html=m.top.content    <-- isn't m.top.content now set ??


>> no, result will be in "m.haamro.HTTPcontent" in "gotContent" sub, not here, and not in "m.top.content".  If you don't want to / can't re-use gotContent sub, then you can set a new different observer on m.haamro to handle the response differently than gotContent:

m.haamro.ObserveField("HTTPcontent","newDifferentFunction")

...

sub newDifferentFunction()
   m.top.content = m.haamro.HTTPcontent
   ' or '
   html = m.haamro.HTTPcontent
   print html
end sub
aspiring
0 Kudos
joetesta
Roku Guru

Re: Returning value from function with Task

Here is the more complete solution with your code:

function getHTMLContent(webpage as string)
   m.haamro = createObject("RoSGNode", "haamroContent")
   m.haamro.HTTPurl=webpage
   m.haamro.functionName="getdata"
   m.haamro.ObserveField("HTTPcontent","gotContent")
   m.haamro.control = "RUN"
end function

sub gotContent()
    m.top.content = m.haamro.HTTPcontent
   print m.top.content '<--- this is working and prints the output'
end sub

function GrabWOD(webpage as string)
   m.haamro.HTTPurl=webpage
   m.haamro.ObserveField("HTTPcontent","newDifferentFunction")
   m.haamro.control = "RUN"
  ' This function can never see the response from the task, it has to use the observeField callback to get it'
end function

sub newDifferentFunction()
    m.top.content = m.haamro.HTTPcontent
    ' or '
    html = m.haamro.HTTPcontent
    print html
end sub
aspiring
0 Kudos
haamro
Visitor

Re: Returning value from function with Task

Getting HTTPcontent works great ! Thank you very much for making it possible. But how do I return the HTTPContent  value from the function itself so that I can use it elsewhere

function show_web_content (webpage)
  ......
return webcontent
end function


function getHTMLContent(webpage as string)   <=== HTTPcontent is not set yet so I cannot return its value from this function
   m.haamro = createObject("RoSGNode", "haamroContent")
   m.haamro.HTTPurl=webpage
   m.haamro.functionName="getdata"
   m.haamro.ObserveField("HTTPcontent","gotContent")
   m.haamro.control = "RUN"
end function

function gotContent() <=== this function is called from above, so not sure how do I return the value from this function to the main function that is making HTTP Request.
    m.top.content = m.haamro.HTTPcontent
   print m.top.content
end function
0 Kudos
joetesta
Roku Guru

Re: Returning value from function with Task

Are you calling this code from Main or from a node?
Assuming this is inside a node, then after you assign m.top.content to the current node's (lets call it NODE) content field, that will be visible externally via NODE.content
Would need to see more code / what you're trying to do to give you exact steps.

Maybe this will help, from inside a node the fields are referenced by "m.top.FIELDNAME" and from outside the same node, the fields can be retrieved via NODE.FIELDNAME where NODE is the node object, usually preceded by "m." so it can be referenced throughout the current scope.
aspiring
0 Kudos
haamro
Visitor

Re: Returning value from function with Task

Thank you very much.
0 Kudos
AKRDev
Reel Rookie

Re: Returning value from function with Task

Please delete this, replied here instead of another post by error.

0 Kudos