Implement undocummented `roWebSocket`
lvcabral opened this issue · 0 comments
lvcabral commented
Code shared on Roku Community Slack by Brahim Hadriche
ws = CreateObject("roWebSocket")
? ws '<Component: roWebSocket>
? type(ws) 'roWebSocket
? GetInterface(ws, "ifWebSocket") '<Interface: ifWebSocket>
? GetInterface(ws, "ifSetMessagePort") '<Interface: ifSetMessagePort>
? GetInterface(ws, "ifGetMessagePort") '<Interface: ifGetMessagePort>
? GetInterface(ws, "ifHttpAgent") '<Interface: ifHttpAgent>
m.port = CreateObject("roMessagePort")
ws.setMessagePort(m.port) ' roWebSocketEvent, roWebSocketEvent.GetSocketID()
ws.SetUrl("ws://192.168.1.182:1337")
? ws.GetUrl()
ws.Send("Hello from Roku!")Code shared on Roku Community Slack by Chad Michael:
Out of curiosity, I checked it out just now and it does indeed seem to work with wss. I just added
ws.Open()to the sample above and gave it an ngrok'd wss url to a local socket server and sure enough, it hit it. Listening to the port you get aroWebSocketEventmsg object. But not sure what methods are on it to get response details, all I found that worked wasmsg.GetInfo()but its kinda useless. Would be awesome if Roku would document this for us. Even if there were "limitations" it would be huge to have wss socket handling!
sub Main()
port = CreateObject("roMessagePort")
ws = CreateObject("roWebSocket")
ws.SetMessagePort(port)
url = "wss://78da1027bfb4.ngrok.app/sockets"
ws.SetUrl(url)
ws.Open()
connected = false
' wait for the socket to open
while true
msg = wait(0, port)
if type(msg) = "roWebSocketEvent" then
if msg.GetType() = 1 then
print "Socket Openned: "; msg.GetInfo()
connected = true
exit while
else
print "Failed to get Open signal. Got type: " + msg.GetType().ToStr(); msg.GetInfo()
exit while
end if
end if
end while
if connected = true then
ws.Send("Hello World")
' wait for message response
while true
msg = wait(0, port)
if type(msg) = "roWebSocketEvent" then
if msg.GetType() = 4 then
print "Pending response "; msg.GetInfo()
else if msg.GetType() = 5 then
print "Got Response: "; msg.GetInfo()
res = msg.GetInfo().text
? res
else
print "Unknown Type: " + msg.GetType().ToStr(); msg.GetInfo()
end if
end if
end while
end if
end sub