connect method Null safety

Future<void> connect()

Implementation

Future<void> connect() async {
	if (_server == null) {
		_applyReconnect = true;
		_queriesTimer ??= Timer.periodic(const Duration(seconds: 1), (_) => _checkQueriesTimeout());
		await onLog?.call('Connecting to $address...');

		InternetAddress? internetAddress;
		if (address is String) internetAddress = InternetAddress.tryParse(address as String);
		else if (address is InternetAddress) internetAddress = address as InternetAddress;
		else throw Exception('Address must be either String or InternetAddress instance');
		try {
			if (internetAddress == null) socket = ConnectMeSocket.ws(await WebSocket.connect(address as String, headers: headers));
			else socket = ConnectMeSocket.tcp(await Socket.connect(internetAddress, port));
		}
		catch (err, stack) {
			onError?.call('Unable to connect: $err', stack);
			if (autoReconnect && _applyReconnect) {
				onError?.call('Unable to connect to $address, reconnect in 3 second...');
				reconnectTimer = Timer(const Duration(seconds: 3), connect);
			}
			return;
		}
		_socketInitialized = true;

		onConnect?.call();
		await onLog?.call('Connection established');
		_listenSocket();
	}
}