Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement undocummented roWebSocket #281

Open
lvcabral opened this issue Mar 6, 2024 · 0 comments
Open

Implement undocummented roWebSocket #281

lvcabral opened this issue Mar 6, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@lvcabral
Copy link
Owner

lvcabral commented Mar 6, 2024

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 a roWebSocketEvent msg object. But not sure what methods are on it to get response details, all I found that worked was msg.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
@lvcabral lvcabral added the enhancement New feature or request label Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant