Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
Cwright017
Level 7

Roku sockets

I'm trying to open a tcp socket from my Roku 3 for the purpose of reporting test results (instead of using the native debug console)

I've copied the example for roStreamSocket - but it doesn't work, at all.



'Run tests on brstest

function main()
print "MAIN...."
messagePort = CreateObject("roMessagePort")
connections = {}
buffer = CreateObject("roByteArray")
buffer[512] = 0
tcpListen = CreateObject("roStreamSocket")
tcpListen.setMessagePort(messagePort)
addr = CreateObject("roSocketAddress")
addr.setPort(54321)

tcpListen.setAddress(addr)
tcpListen.notifyReadable(true)
tcpListen.notifyWritable(true)

x = tcpListen.listen(4)

if not tcpListen.eOK()
print "Error creating listen socket"
stop
end if

while True
event = wait(0, messagePort)

if type(event) = "roSocketEvent"
print "GOT HERE"
changedID = event.getSocketID()

if changedID = tcpListen.getID() and tcpListen.isReadable()
newConnection = tcpListen.accept()

if newConnection = Invalid
print "accept failed"
else
print "accepted new connection " newConnection.getID()
newConnection.notifyReadable(true)
newConnection.setMessagePort(messagePort)
connections[Stri(newConnection.getID())] = newConnection
end if

else
' Activity on an open connection
connection = connections[Stri(changedID)]
closed = False

if connection.isReadable()
received = connection.receive(buffer, 0, 512)
print "received is " received

if received > 0
print "Echo input: '"; buffer.ToAsciiString(); "'"
' If we are unable to send, just drop data for now.
' You could use notifywritable and buffer data, but that is
' omitted for clarity.
connection.send(buffer, 0, received)
else if received=0 ' client closed
closed = True
end if
end if

if closed or not connection.eOK()
print "closing connection " changedID
connection.close()
connections.delete(Stri(changedID))
end if
end if
end if
end while

print "Main loop exited"
tcpListen.close()

for each id in connections
connections[id].close()
end for

end function



Is what I have in my main.brs - and it listens, and I seem to be able to telnet to the port - but it reports no connection event, nor does it report any data sent to it - and the event isn't of type string, but of type function :/
0 Kudos
5 Replies
belltown
Level 7

Re: Roku sockets

I'm not sure why it didn't work for you. I ran your code (unmodified), then typed telnet 192.168.0.6 54321 followed by hello. The debug output from the code (on port 8085) showed:


------ Compiling dev 'Socket Test' ------

------ Running dev 'Socket Test' main ------
MAIN....
GOT HERE
accepted new connection 87031814
GOT HERE
received is 1
Echo input: 'h'
GOT HERE
received is 1
Echo input: 'e'
GOT HERE
received is 1
Echo input: 'l'
GOT HERE
received is 1
Echo input: 'l'
GOT HERE
received is 1
Echo input: 'o'


Each character typed was echoed back to the telnet console on port 54321.

What output are you seeing from the debugger on port 8085?
https://github.com/belltown/
0 Kudos
Cwright017
Level 7

Re: Roku sockets

Hey thanks for the quick reply.

My output is just
MAIN...


and when I telnet I get


$ telnet 10.65.94.89 54321
Trying 10.65.94.89...
Connected to 10.65.94.89.
Escape character is '^]'.
hello


but no output on the debug port - not even a connection event.

What firmware are you running? I'm using 7.1 build 4061. Just tried on a Roku 4 and that too didn't work :/
0 Kudos
Roku Employee
Roku Employee

Re: Roku sockets

"Cwright017" wrote:
I'm trying to open a tcp socket from my Roku 3 for the purpose of reporting test results (instead of using the native debug console)

I've copied the example for roStreamSocket - but it doesn't work, at all.

...

Is what I have in my main.brs - and it listens, and I seem to be able to telnet to the port - but it reports no connection event, nor does it report any data sent to it - and the event isn't of type string, but of type function :/


Are you literally running just the above code, or are you running in a larger context with some other code?

If the latter, make sure you don't have a function/sub named 'event' somewhere. Smiley Happy
0 Kudos
EnTerr
Level 8

Re: Roku sockets

"RokuKC" wrote:
If the latter, make sure you don't have a function/sub named 'event' somewhere. Smiley Happy

Oooh, that's a good one! It throws us at the name scoping question, doesn't it.
0 Kudos
Cwright017
Level 7

Re: Roku sockets

Argh - I did have another function called event! It now all works fine.

But on the topic of namespacing - is this something Roku plan to introduce in the near future? Maybe classes?
0 Kudos