[2.4.1.151] Multicast socket how-to - drivers only

Hubitat platform now supports multicast sockets. The interface is identical to the one used by regular sockets. Below is a sample driver that sends a message (and receives it back right away).

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(int 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}")
}
6 Likes