restaurant_local_server 1.0.2
restaurant_local_server: ^1.0.2 copied to clipboard
A generic Flutter package for building local server applications with UDP discovery, HTTP REST APIs, WebSocket support, and real-time synchronization capabilities.
import 'package:restaurant_local_server/restaurant_local_server.dart';
/// Basic example showing how to create and start a local server
/// with HTTP API, WebSocket, and UDP discovery capabilities.
void main() async {
print('š Starting Restaurant Local Server Example');
try {
// Create server configuration
final config = LocalServerConfig(
serverName: 'Example Restaurant Server',
serverVersion: '1.0.0',
capabilities: {
'orders': true,
'tables': true,
'real_time_sync': true,
},
// Network configuration
httpConfig: const HttpServerConfig(
httpPort: 8080,
enableCors: true,
),
webSocketConfig: const WebSocketServerConfig(
port: 8081,
heartbeatIntervalSeconds: 30,
),
discoveryConfig: const DiscoveryConfig(
discoveryPort: 8082,
broadcastInterval: Duration(seconds: 5),
),
// Health monitoring and recovery
enableAutoRecovery: true,
healthCheckIntervalSeconds: 30,
maxRecoveryAttempts: 3,
enableLogging: true,
);
// Create event handlers
final eventHandlers = LocalServerEventHandlers(
onStatusChange: (oldStatus, newStatus) {
print('š Server Status Changed: $oldStatus -> $newStatus');
},
onHealthChange: (oldHealth, newHealth) {
print('ā¤ļø Server Health: $oldHealth -> $newHealth');
},
onClientConnect: (clientId, clientInfo) {
print('š Client Connected: $clientId');
print(' Remote Address: ${clientInfo.remoteAddress}');
print(' User Agent: ${clientInfo.userAgent}');
},
onClientDisconnect: (clientId, reason) {
print('š Client Disconnected: $clientId - $reason');
},
onError: (error, details) {
print('ā Server Error: $error');
if (details != null) print(' Details: $details');
},
onRecoveryAttempt: (service, attemptNumber) {
print('š Recovery Attempt $attemptNumber for $service');
},
);
// Create and configure server manager
final serverManager = LocalServerManager(
config: config,
eventHandlers: eventHandlers,
);
// Start the server
print('š§ Starting all services...');
await serverManager.start();
// Display server information
final serverInfo = serverManager.serverInfo;
print('\nā
Server Started Successfully!');
print('š Server Details:');
if (serverInfo != null) {
print(' Name: ${serverInfo.name}');
print(' Version: ${serverInfo.version}');
print(' IP Address: ${serverInfo.ipAddress}');
print(' HTTP Port: ${serverInfo.httpPort}');
print(' WebSocket Port: ${serverInfo.webSocketPort}');
print(' Capabilities: ${serverInfo.capabilities.keys.join(', ')}');
}
// Display access URLs
if (serverInfo != null) {
print('\nš Access URLs:');
print(' HTTP API: ${serverInfo.httpUrl}');
print(' WebSocket: ${serverInfo.webSocketUrl}');
print(' Health Check: ${serverInfo.httpUrl}/health');
}
// Display server statistics periodically
print('\nš Server Statistics:');
_startStatsDisplay(serverManager);
// Simulate some server activity
_simulateServerActivity(serverManager);
print('\nšÆ Server is running! Press Ctrl+C to stop.');
print('š” Try connecting clients or accessing the HTTP endpoints.');
} catch (e, stackTrace) {
print('š„ Failed to start server: $e');
print('Stack trace: $stackTrace');
}
}
/// Periodically display server statistics
void _startStatsDisplay(LocalServerManager serverManager) {
// Update stats every 10 seconds
Stream.periodic(Duration(seconds: 10)).listen((_) async {
final stats = serverManager.getStatistics();
print('\nš Server Stats Update:');
print(' Status: ${stats.status}');
print(' Health: ${stats.healthStatus}');
print(' Uptime: ${stats.uptime}');
print(' Connected Clients: ${stats.connectedClients}');
print(' Service Health: ${stats.serviceHealth.keys.join(', ')}');
if (stats.connectedClients > 0) {
print(' Connected Client Count: ${stats.connectedClients}');
}
});
}
/// Simulate some server activity for demonstration
void _simulateServerActivity(LocalServerManager serverManager) {
// Simulate periodic system messages
Stream.periodic(Duration(seconds: 30)).listen((_) {
final messages = [
'System health check completed',
'Database backup in progress',
'New feature deployed successfully',
'Scheduled maintenance reminder',
'Performance optimization applied',
];
final message = messages[DateTime.now().millisecond % messages.length];
serverManager.broadcastSystemMessage(message: message);
print('š¢ Broadcasted: $message');
});
// Simulate periodic entity updates
Stream.periodic(Duration(seconds: 20)).listen((_) {
final entityTypes = ['order', 'table', 'product', 'user'];
final statuses = ['active', 'pending', 'completed', 'cancelled'];
final entityType = entityTypes[DateTime.now().millisecond % entityTypes.length];
final status = statuses[DateTime.now().second % statuses.length];
final entityId = DateTime.now().millisecondsSinceEpoch.toString();
serverManager.broadcastEntityStatusUpdate(
entityType: entityType,
entityId: entityId,
status: status,
additionalData: {
'timestamp': DateTime.now().toIso8601String(),
'source': 'simulation',
},
);
print('š Simulated $entityType update: $status');
});
}