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

Hit a simple URL with code?

I just need to hit a simple URL prior to displaying content, but I don't want to hold up the video from playing (in case it doesn't return). The plan is obviously to use this for a simple analytics event, but just not sure of a simple way to do it.

Thanks for the help.
0 Kudos
6 REPLIES 6
agmark
Visitor

Re: Hit a simple URL with code?

belltown posted a very good routine to check the response code from a url. It could be easily adapted for your use.
http://forums.roku.com/viewtopic.php?f=34&t=47659&start=30#p327128
0 Kudos
belltown
Roku Guru

Re: Hit a simple URL with code?

"agmark" wrote:
belltown posted a very good routine to check the response code from a url. It could be easily adapted for your use.
http://forums.roku.com/viewtopic.php?f=34&t=47659&start=30#p327128

That function should do what you want. It sends an request to a url asynchronously. When you call it pass in a timeout value (in milliseconds) instead of zero (wait forever).
0 Kudos
jeremyk
Visitor

Re: Hit a simple URL with code?

Yup, this is perfect. Thanks for the quick response!
0 Kudos
RokuJoel
Binge Watcher

Re: Hit a simple URL with code?

Create an RoURLTransfer object, and set it's port to the same port as your videoscreen. Give your wait statement a time like 100 milliseconds instead of 0. Add a check for the returned value, for example:


Function showVideoScreen(episode As Object)

if type(episode) <> "roAssociativeArray" then
print "invalid data passed to showVideoScreen"
return -1
endif

port = CreateObject("roMessagePort")
screen = CreateObject("roVideoScreen")
screen.SetMessagePort(port)

screen.SetPositionNotificationPeriod(30)
screen.SetContent(episode)


here is the URL Transfer setup:


xfer=createobject("roURLTransfer")
xfer.seturl("http://myserver.com/check.php")
xfer.setport(port)
xfer.asyncgettostring()


screen.Show()

'Uncomment his line to dump the contents of the episode to be played
'PrintAA(episode)




then the while loop with a non-zero wait time (here, i've set it to 100 milliseconds), and I've made it the first message-type check:

    while true
msg = wait(100, port)
if type(msg)="roUrlEvent" then
str=msg.getstring()
print "************returned url data************"
print str
else if type(msg) = "roVideoScreenEvent" then
?"******************************************MESSAGE**********************************"
?"message:";msg.getmessage();" Type:";msg.gettype();" Index:"; msg.getindex();" data:";msg.getdata();" playback pos? ";msg.isplaybackposition()
?"******************************************MESSAGE**********************************"
'print "showHomeScreen | msg = "; msg.getMessage() " | index = "; msg.GetIndex()
if msg.isScreenClosed()
print "Screen closed"
exit while
elseif msg.isRequestFailed()
print "Video request failure: "; msg.GetIndex(); " " msg.GetData()
elseif msg.isStatusMessage()
print "Video status: "; msg.GetIndex(); " " msg.GetData()
elseif msg.isButtonPressed()
print "Button pressed: "; msg.GetIndex(); " " msg.GetData()
elseif msg.isPlaybackPosition() then
nowpos = msg.GetIndex()
RegWrite(episode.ContentId, nowpos.toStr())
else if msg.isStatusMessage() then
?"status message"
?"message:";msg.getmessage();" Type:";msg.gettype();" Index:"; msg.getindex();" data:";msg.getdata()
else if msg.isstreamstarted() then
?"StreamStarted"
?"message:";msg.getmessage();" Type:";msg.gettype();" Index:"; msg.getindex();" data:";msg.getdata()
else
print "Unexpected event type: "; msg.GetType()
end if
end if
end while

End Function


This way, you can have multiple things going on - video is playing and, you are also checking for a URL Event.

- Joel
0 Kudos
jvalero
Visitor

Re: Hit a simple URL with code?

I'm trying to check that my URL transfers are working as well, and this post was very helpful in understanding the proper approach to do so. However, my scenario is slightly different in that I want to both call a URL at the beginning of playback, but also at certain increments of progress as well. Simply put, I need to call some impression URLs before the video starts, and then I also need to call some additional progress related urls at for example 25%, 50%, 75%, completion.

To handle the progress side of things I've setup some code to fire off as the playback position reaches a certain milestone.

Here is a stripped down version of this part of my loop:
If marker = "midpoint" And Int((m.position / m.duration) * 100) > 49 And m.vastMp = False

PrettyPrint("Called Midpoint Url: " + url) '// PrettyPrint() = Custom function to print out formatted debug info.
vastProgressUrl.SetUrl(url)
vastProgressUrl.AsyncGetToString()
m.vastMp = True '// This just makes sure that each milestone is printed once

End If


I create the vastProgressUrl right before the overall event loop for the port:
vastProgressUrl = CreateObject("roUrlTransfer")
vastProgressUrl.SetPort(messagePort)


And I have the following in my event loop:
msg = wait(100, messagePort)

'// URL Transfer Events
If Type(msg) = "roUrlEvent" Then

'// And if response code == OK
If msg.GetResponseCode() = 200 Then

'// Assign Response
Print "Success: " msg.GetResponseCode()
Else

Print "Failed response. Reponse Code: " + msg.GetResponseCode()

End If

End If


For some reason I'm not seeing anything back from this pass or fail. So I'm wondering if it is an issue with the loop and me making the URL requests inside of it. Any ideas?
0 Kudos
jvalero
Visitor

Re: Hit a simple URL with code?

I managed to figure it out. I had to setup another port to listen in on the url events. I'm now getting back success and fail messages.
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.