AsyncPostFromStringto do a non-blocking request.
AsyncPostFromStringas it is suppose to be non-blocking but that does not seem to be the case. I have the service wait for 5 seconds to verify it's non-blocking but it always waits until the logger request is complete.
Channel Initialized - START Logger
Waiting for response...
Sucessful Response
END Logger
InitTheme Start - InitTheme End - MainContentList Start - MainContentList End
Channel Initialized - START Logger
Waiting for response...
InitTheme Start - InitTheme End - MainContentList Start - MainContentList End
Sucessful Response
END Logger
request.setport(port)and I tested also with
request.setMessagePort(port)but no change.
Sub Logger(event, value="")
print "START Logger"
' Get Device Info
di = CreateObject("roDeviceInfo")
' Make sure there is an active connection
if (di.GetLinkStatus()) then
' Define timeout
timeout = 2000
' Define URL
url="https://my.super.secret.url"
' Get Device Info
di = CreateObject("roDeviceInfo")
' Create transfer object
request=createobject("roURLTransfer")
request.SetCertificatesFile("common:/certs/ca-bundle.crt")
request.InitClientCertificates()
request.AddHeader("Content-Type", "application/json")
request.seturl(url)
' Event monitoring
port=createobject("roMessagePort")
request.setport(port)
' Timer for timeout
timer=createobject("roTimeSpan")
timer.mark()
' Build data object
data = {
"entityName": "Roku App",
"entityEvent": event,
' Device info (https://sdkdocs.roku.com/display/sdkdoc/ifDeviceInfo)
"entityID": di.GetDeviceUniqueId(),
"entityMeta": {
"modelDisplayName": di.GetModelDisplayName(),
"model": di.GetModel(),
"softwareVersion": di.GetVersion(),
"displayType": di.GetDisplayType(),
"displayMode": di.GetDisplayMode(),
"displayAspectRatio": di.GetDisplayAspectRatio(),
"timezone": di.GetTimeZone(),
"ip": di.GetIPAddrs(),
"locale": di.GetCurrentLocale(),
"countryCode": di.GetCountryCode(),
"timeSinceLastKeyPress": di.TimeSinceLastKeypress(),
"videoPlaybackResolution": di.GetVideoMode(),
"isHDMIConnected": di.IsHDMIConnected(),
"audioOutputChannel": di.GetAudioOutputChannel(),
"activeNetwork": di.GetLinkStatus(),
"connectionType": di.GetConnectionType()
}
}
' Only add value if provided
if (value <> "") then
data.entityValue = value
end if
' Format to JSON
jsonData = FormatJson(data)
print "jsonData: " ; jsonData
' Send Requests
request.AsyncPostFromString(jsonData)
print "Waiting for response..."
' Gracefully handle the request
while true
msg=wait(100,port) '100 millisecond pause
if type(msg)="roUrlEvent" then
if msg.getresponsecode()=200 then
print "Sucessful Response"
exit while
else
print "Response Code" ; msg.getresponsecode()
request.asynccancel()
end if
end if
' Set timeout
if timer.totalmilliseconds() > timeout then
print "timeout exceeded"
exit while
end if
end while
else
print "No active Internet connection. Logger is being skipped."
end if
print "END Logger"
End Sub
"SoN9ne" wrote:
One last question... I can no longer do a timeout since this is in the main loop. There are multiple roUrlEvent's so there isn't a way (that I noticed) to identify which is which. Is this something I need to handle in the code or does roUrlEvent handle this internally? Personally, I'd like to log the event for later use but that's not really an issue.
Your question helps me to make a POST request. Thanks.