connect method
connect let you connect to a network printer.
Sets TCP_NODELAY to disable Nagle's algorithm so small ESC/POS commands are sent immediately without waiting for more data.
Implementation
@override
Future connect({Duration? timeout = const Duration(seconds: 5)}) async {
// Avoid opening multiple connections.
if (isConnected) return;
try {
socket = await Socket.connect(
printer.address,
printer.port ?? 9100,
timeout: timeout,
);
// Disable Nagle's algorithm — send ESC/POS commands immediately
// instead of buffering them. Critical for small writes like status
// queries and cut commands.
socket!.setOption(SocketOption.tcpNoDelay, true);
} catch (e) {
socket = null;
return Future.error('Failed to connect to ${printer.address}:${printer.port ?? 9100} — $e');
}
}