47 lines
1.3 KiB
Dart
47 lines
1.3 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:shelf/shelf.dart';
|
|
import 'package:shelf/shelf_io.dart' as io;
|
|
import 'package:shelf_router/shelf_router.dart';
|
|
|
|
class ReceiverServer {
|
|
HttpServer? _server;
|
|
final Function(Map<String, dynamic>) onPlayRequest;
|
|
|
|
ReceiverServer({required this.onPlayRequest});
|
|
|
|
Future<int> start() async {
|
|
final router = Router();
|
|
|
|
router.post('/play', (Request request) async {
|
|
final payload = await request.readAsString();
|
|
try {
|
|
final data = jsonDecode(payload) as Map<String, dynamic>;
|
|
onPlayRequest(data);
|
|
return Response.ok(jsonEncode({'status': 'success'}));
|
|
} catch (e) {
|
|
return Response.badRequest(body: jsonEncode({'error': 'Invalid JSON'}));
|
|
}
|
|
});
|
|
|
|
router.get('/health', (Request request) {
|
|
return Response.ok(jsonEncode({'status': 'ok'}));
|
|
});
|
|
|
|
_server = await io.serve(router, InternetAddress.anyIPv4, 0); // Port 0 for auto-assign or fixed port
|
|
print('Receiver Server running on port ${_server!.port}');
|
|
return _server!.port;
|
|
}
|
|
|
|
Future<void> stop() async {
|
|
await _server?.close();
|
|
_server = null;
|
|
}
|
|
|
|
String? get ipAddress {
|
|
// Note: This is a simplified way to get local IP.
|
|
// In a real app, you might want to filter for the Wi-Fi interface.
|
|
return 'Local IP';
|
|
}
|
|
}
|