import 'dart:async'; import 'package:nsd/nsd.dart'; abstract class IDiscoveryService { Future registerService(int port); Future stopRegistration(); Stream> discoverServices(); } class DiscoveryService implements IDiscoveryService { Registration? _registration; final String _serviceType = '_http._tcp'; final String _serviceName = 'FastWatcher'; @override Future registerService(int port) async { _registration = await register( Service( name: _serviceName, type: _serviceType, port: port, ), ); } @override Future stopRegistration() async { if (_registration != null) { await unregister(_registration!); _registration = null; } } @override Stream> discoverServices() async* { final discovery = await startDiscovery(_serviceType); final controller = StreamController>(); void update() { controller.add(discovery.services); } discovery.addListener(update); yield* controller.stream; // Note: In a real app, handle disposal } }