The roDataGramSocket component enables Brightscript apps to send and receive UDP packets. The interface is modeled on and works much like standard Berkeley sockets.
Supported Interfaces
Note: some legacy firmware versions may implement ifSocketCastOption as ifSocketCast.
Supported Events
Description
This object is created without any arguments:
- CreateObject("roDataGramSocket")
Example
' UDP 2-way peer-to-peer asynchronous comm on port 54321
' periodically sends out a message to a specific address and port
' prints any message it receives
Function UDPPeer()
msgPort = createobject("roMessagePort")
udp = createobject("roDatagramSocket")
udp.setMessagePort(msgPort) 'notifications for udp come to msgPort
addr = createobject("roSocketAddress")
addr.setPort(54321)
udp.setAddress(addr) ' bind to all host addresses on port 54321
addr.SetHostName("10.1.1.1")
udp.setSendToAddress(addr) ' peer IP and port
udp.notifyReadable(true)
timeout = 1 * 10 * 1000 ' ten seconds in milliseconds
deviceName = Createobject("roDeviceInfo").GetFriendlyName()
message = "Datagram from " + deviceName
udp.sendStr(message)
continue = udp.eOK()
While continue
event = wait(timeout, msgPort)
If type(event)="roSocketEvent"
If event.getSocketID()=udp.getID()
If udp.isReadable()
message = udp.receiveStr(512) ' max 512 characters
print "Received message: '"; message; "'"
End If
End If
Else If event=invalid
print "Timeout"
udp.sendStr(message) ' periodic send
End If
End While
udp.close() ' would happen automatically as udp goes out of scope
End Function