import 'dart:async'; import 'package:stream_channel/stream_channel.dart'; import 'transport.dart'; class RemoteClient extends StreamChannelMixin { final StreamChannel channel; final StreamChannelController _ctrl = StreamChannelController(); RemoteClient.withoutJson(this.channel) { _ctrl.local.stream .map((m) => m.toJson()) .cast() .forEach(channel.sink.add); channel.stream.listen((m) { _ctrl.local.sink.add(OperationMessage.fromJson(m)); }); } RemoteClient(StreamChannel channel) : this.withoutJson(jsonDocument.bind(channel).cast()); @override StreamSink get sink => _ctrl.foreign.sink; @override Stream get stream => _ctrl.foreign.stream; void close() { channel.sink.close(); _ctrl.local.sink.close(); } }