connect method

  1. @override
Future<void> connect({
  1. RawMessage? lastWill,
})
override

Connects to the broker.

lastWill is published by the broker if this connection drops without a disconnect. Throws a ConnectionException if the broker can't be reached or refuses the connection.

Implementation

@override
Future<void> connect({RawMessage? lastWill}) async {
  if (_state == TransportState.connected) return;
  if (lastWill != null) checkTopicName(lastWill.topic);

  _setState(TransportState.connecting);
  if (!broker._running) {
    _setState(TransportState.disconnected);
    throw const ConnectionException('Broker is not running');
  }
  _lastWill = lastWill;
  broker._clients.add(this);
  _setState(TransportState.connected);
}