crdt_socket_sync 0.2.0
crdt_socket_sync: ^0.2.0 copied to clipboard
Conflict-free replicated data type (CRDT) - Socket sync implementation provided in dart
// ignore_for_file: avoid_print just an example
import 'dart:async';
import 'dart:io';
import 'package:crdt_socket_sync/web_socket_server.dart';
import 'server_registry.dart';
late WebSocketServer server;
void main(List<String> args) async {
print('π Starting CRDT WebSocket Server...');
server = WebSocketServer(
serverFactory: () => HttpServer.bind(InternetAddress.anyIPv4.host, 8080),
serverRegistry: serverRegistry,
plugins: [
ServerAwarenessPlugin(),
],
);
_setupSigintHandler();
await _startServer();
}
void _setupSigintHandler() {
ProcessSignal.sigint.watch().listen((signal) async {
print('\nβΉοΈ Received SIGINT, shutting down gracefully...');
await server.stop();
print('β
Server stopped.');
exit(0);
});
}
Future<void> _startServer() async {
try {
server.serverEvents.listen((event) {
print('β‘ Server event: $event');
});
await server.start();
print('β
CRDT WebSocket Server started successfully!');
print('π‘ Listening on ${server.host}:${server.port}');
print('π‘ Press Ctrl+C to stop the server');
// Keep the server running indefinitely
print('π Server is running... waiting for connections');
final completer = Completer<void>();
await completer.future;
return;
} catch (e) {
print('β Failed to start server: $e');
exit(1);
}
}