connectToServer method
Connects to the specified server.
Specify isSecure if you do not want to connect to a secure service.
Specify timeout to specify a different timeout for the connection.
This defaults to 20 seconds.
Implementation
Future<ConnectionInfo> connectToServer(
String host,
int port, {
bool isSecure = true,
Duration timeout = const Duration(seconds: 20),
}) async {
logApp(
'connecting to server $host:$port - '
'secure: $isSecure, timeout: $timeout',
);
connectionInfo = ConnectionInfo(host, port, isSecure: isSecure);
final socket = isSecure
? await SecureSocket.connect(
host,
port,
onBadCertificate: onBadCertificate,
context: securityContext,
).timeout(timeout)
: await Socket.connect(host, port).timeout(timeout);
final greetingsCompleter = Completer<ConnectionInfo>();
_greetingsCompleter = greetingsCompleter;
_isServerGreetingDone = false;
connect(socket);
// the server greeting has to arrive within the timeout as well,
// otherwise e.g. a TLS port contacted without TLS would hang forever
return greetingsCompleter.future.timeout(
timeout,
onTimeout: () {
isSocketClosingExpected = true;
_isConnected = false;
socket.destroy();
throw createClientError(
'no server greeting received from $host:$port within $timeout',
);
},
);
}