Future init([bool broadcast = false ])

Source

Future init([bool broadcast = false]) async {
  _socket = await RawDatagramSocket.bind("0.0.0.0", broadcast ? 1900 : 0);

  _socket.multicastHops = 10;
  _socket.broadcastEnabled = true;
  _socket.listen((RawSocketEvent event) {
    if (event == RawSocketEvent.READ) {
      var packet = _socket.receive();
      _socket.writeEventsEnabled = true;

      if (packet == null) {
        return;
      }

      var data = UTF8.decode(packet.data);
      _onMessage(packet, data);
    } else if (event == RawSocketEvent.CLOSED) {
      if (!_brokerController.isClosed) {
        _brokerController.close();
      }
    }
  });

  _socket.writeEventsEnabled = true;

  var interfaces = await NetworkInterface.list();
  try {
    for (var interface in interfaces) {
      try {
        _socket.joinMulticast(new InternetAddress("239.255.255.230"), interface: interface);
      } catch (e) {
        _socket.joinMulticast(new InternetAddress("239.255.255.230"), interface: interface);
      }
    }
  } catch (e) {
    _socket.joinMulticast(new InternetAddress("239.255.255.230"));
  }
}