handshakeInstance static method

Future<IShspInstance> handshakeInstance(
  1. IShspInstance instance,
  2. ShspHandshakeHandlerOptions options, [
  3. void onOpen(
    1. IShspInstance instance
    )?
])

Perform handshake procedure and return when connection is open

Implementation

static Future<IShspInstance> handshakeInstance(
    IShspInstance instance, ShspHandshakeHandlerOptions options,
    [void Function(IShspInstance instance)? onOpen]) async {
  final int maxMs = options.timeoutMs;
  int elapsed = 0;
  final int interval = options.intervalOfSendingHandshakeMs;

  while (elapsed < maxMs) {
    instance.sendHandshake();
    await Future.delayed(Duration(milliseconds: interval));
    elapsed += interval;
    if (instance.open) {
      if (onOpen != null) onOpen(instance);
      break; // connection open so i quit
    }
  }
  return instance;
}