attach method

Future<void> attach(
  1. Stream<Map<String, dynamic>> frames,
  2. WireFrameSink send
)

Serves one client until it detaches (its frame stream ends or errors) or the server shuts down. A second simultaneous attach is rejected with already_attached before its handshake is read.

Implementation

Future<void> attach(Stream<Map<String, dynamic>> frames, WireFrameSink send) {
  if (_shutDown) {
    send(_error('shutting_down', 'wire-serve is shutting down'));
    return Future<void>.value();
  }
  if (_client != null || _attaching) {
    send(
      _error(
        'already_attached',
        'another client is attached to this session; '
            'retry after it disconnects',
      ),
    );
    return Future<void>.value();
  }
  _attaching = true;
  final done = Completer<void>();
  _attachDone = done;
  _frames = frames.listen(
    (frame) => _onClientFrame(frame, send),
    onDone: () => _detach(),
    onError: (Object error) => _detach(error),
    cancelOnError: true,
  );
  return done.future;
}