Skip to content

Multicast

ComputerElite edited this page Jan 23, 2022 · 4 revisions

Getting Mod Info and Connection Information

  • The mod will send a Multicast Response out to the network on 232.0.53.5:53500 using the UDP Protocol.
  • Any application wanting to receive the response has to subscribe to that IP and listen on the port.

Here's a simple NodeJS example for it

// This will subscribe all network interfaces to 232.0.53.5:53500
GetLocalIPs().forEach(ip => {
    SetupMulticast(ip)
})

function SetupMulticast(localIP) {
    var PORT = 53500;
    var HOST = localIP;
    var MCASTIP = '232.0.53.5';
    var dgram = require('dgram');
    var client = dgram.createSocket('udp4');

    client.on('listening', function () {
        var address = client.address();
        client.setBroadcast(true)
        client.setMulticastTTL(128); 
        client.addMembership(MCASTIP, HOST);
        console.log('UDP Client listening on ' + address.address + ":" + address.port);
    });

    client.on('message', function (message, remote) {   
        console.log('Recieved multicast: ' + remote.address + ':' + remote.port +' - ' + message);
        ipInQueue = remote.address;
    });

    client.bind(PORT, HOST);
}

function GetLocalIPs() {                    // This will get the IPs of all network interfaces
	const  { networkInterfaces }  = require('os')
    const nets = networkInterfaces();
    const results = [];

    for (const name of Object.keys(nets)) {
        for (const net of nets[name]) {
            // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
            if (net.family === 'IPv4' && !net.internal) {
                results.push(net.address);
                console.log("adding " + net.address)
            }
        }
    }
    return results
}
This should give a response similar to this
{
    "ModID":"streamer-tools",
    "ModVersion":"0.1.0",
    "Socket":"192.168.188.30:53501",
    "HTTP":"192.168.188.30:53502",
    "Socketv6":"2345:0425:2CA1:0000:0000:0567:5673:23b5:53501",
    "HTTPv6":"[2345:0425:2CA1:0000:0000:0567:5673:23b5]:53502"
}