send method

Future send (
  1. Packet packet,
  2. Device device,
  3. {bool ignoreIfNotConnected: false}
)

Hosts can send messages to other devices using the internal socket contained in the Device.

Messages can mostly only be sent to Devices received after an event is received from ConnectionListener.onConnected which is triggered when a Client uses Client.connectTo to initiate a connection. You cannot send messages to Devices received in the Discovery phases from DeviceDiscoveryListener.onDiscovery or DeviceDiscoveryListener.onAdvertisement. On the Server can send unreliable broadcast messages to the multicast group by using Server.broadcast.

When ignoreIfNotConnected is set to true, If the device is not connected, it will be silently ignore. If however the value is false which is the default, an Exception will be thrown.

Implementation

send(Packet packet, Device device, {bool ignoreIfNotConnected = false}) async{
  try {
    // check if we have a socket for the device
    if (device.connected) {
      if (packet.isStream) {
        await device._socket.addStream(packet.stream);
        await device._socket.flush();
      }
      else {
        await device._socket.add(packet.bytes);
        await device._socket.flush();
      }
    }
    else if (!ignoreIfNotConnected)
      throw "Cannot send message. There is no socket connection to this Device.";
  }
  catch(ignored){}
}