send method

void send(
  1. Object msg, {
  2. dynamic appendTerminator = true,
})

will send a message to the socket. If the message is a list of bytes, (i.e. Uint8List or List<int>) the bytes will be sent directly.

Otherwise, the message will be converted to a string with Object.toString, encoded to UTF-8, and then sent to the socket.

The terminatorBytes, if set, will be automatically appended to the end of the message, unless appendTerminator is set to false.

Implementation

void send(Object msg, {appendTerminator = true}) {
  if (_socket == null) {
    throw TcpNotReady('socket not created... did you forget to call `connect` or `connectPersistent`?');
  }
  late Uint8List bytes;
  if (msg is Uint8List) {
    bytes = msg;
  } else if (msg is List<int>) {
    bytes = Uint8List.fromList(msg);
  } else {
    bytes = Uint8List.fromList(utf8.encode(msg.toString()));
  }

  if (terminatorBytes != null && appendTerminator) {
    bytes = Uint8List.fromList([...bytes, ...terminatorBytes!]);
  }
  dbg(bytes);
  add(bytes);
}