connect method

Future<void> connect({
  1. ConnectionOptions? connectionOptions,
  2. void onClusterupdate(
    1. ServerInfo? info
    )?,
})

Connects to the given NATS url

var client = NatsClient("localhost", 4222);
var options = ConnectionOptions()
options
 ..verbose = true
 ..pedantic = false
 ..tlsRequired = false
await client.connect(connectionOptions: options);

Implementation

Future<void> connect({
  ConnectionOptions? connectionOptions,
  void onClusterupdate(ServerInfo? info)?,
}) async {
  final _socket = await this.tcpClient.connect();

  this._protocolHandler = ProtocolHandler(socket: _socket, log: log);

  utf8.decoder.bind(_socket).transform(messagesTransformer).listen(
    (String data) {
      if (data.startsWith(INFO)) {
        _setServerInfo(data, connectionOptions);
        if (onClusterupdate != null) onClusterupdate(_serverInfo);
        return;
      }
      _serverPushString(data);
    },
    onDone: () {
      log.info("Host down. Switching to next available host in cluster");
      _removeCurrentHostFromServerInfo(_currentHost, _currentPort);
      _reconnectToNextAvailableInCluster(
          opts: connectionOptions, onClusterupdate: onClusterupdate);
    },
  );
}