Multicast Socket Interface
Send and receive data over multicast sockets using an interface similar to the one for regular sockets
Multicast Socket Interface
The driver environment offers an interface for connecting to a UDP multicast socket and sending and receiving data over that connection.
This interface is not available in platform versions prior to version 2.4.1.
Hubitat-Provided Methods
getMutlicastSocket
This method is part of interfaces, so the full call is interfaces.getMulticastSocket().
Signature
void getMutlicastSocket(String ip, int port)
Parameters
ip- The IP address to connect to.port- The port to connect to.
connect
This method, and all of the below, are methods on the socket object itself (which you can retrieve from the getMutlicastSocket() call above).
Signature
void connect()void connect(Map options)
Parameters
options- Optional parameters to configure the socket connection, same as regular socket interface
Examples
def socket = interfaces.getMulticastSocket("239.0.0.1", port)
if (!socket.connected) socket.connect()
close
Signature
void close()
sendMessage
Signature
void sendMessage(String message)
Parameters
message - The message to send. If this connection is a byte interface, then this must be a hex encoded String.
User-Defined Methods
Methods with the following signatures must be provided in your code and will be called as described.
parse(String message)- This method is called with any incoming messages from the socket server. This is a standard method for drivers.socketStatus(String message)- This method is called with any status messages from the socket client connection (disconnections, errors during connect, etc)
Example
The small driver below demonstrates how to obtain a multicast socket (actually two different ones), connect to it, and send and receive a message over that socket.
metadata {
definition (name: "MulticastSocketeer", namespace: "hubitat", author: "Victor U.") {
command "testSocket1"
command "testSocket2"
}
}
import hubitat.helper.HexUtils
void testSocket1() {
testSocket(5055)
}
void testSocket2() {
testSocket(5058)
}
void testSocket(Integer port) {
log.debug "testSocket-${port}"
def socket = interfaces.getMulticastSocket("239.0.0.1", port)
if (!socket.connected) socket.connect()
socket.sendMessage(HexUtils.byteArrayToHexString("hello - ${port}!".getBytes()))
}
void socketStatus(message) {
log.warn "socket status is: ${message}"
}
void parse(message) {
log.info "received: ${message}"
}
See the Community forum for more information.