init method

Future<void> init({
  1. int port = kDefaultPort,
  2. int maxPlayers = 10,
  3. bool verbose = false,
  4. bool autosave = false,
  5. String description = '',
})

Implementation

Future<void> init({
  int port = kDefaultPort,
  int maxPlayers = 10,
  bool verbose = false,
  bool autosave = false,
  String description = '',
}) async {
  if (verbose) {
    consoler.minLogLevel = LogLevel.verbose;
  }
  log("Starting server on port $port", level: LogLevel.info);
  log('Verbose logging activated', level: LogLevel.verbose);
  this.autosave = autosave;
  _maxPlayers = maxPlayers;
  SecurityContext? securityContext;
  try {
    final privateKey = await File('certs/server.key').readAsBytes();
    final certificate = await File('certs/server.crt').readAsBytes();
    securityContext = SecurityContext()
      ..usePrivateKeyBytes(privateKey)
      ..useCertificateChainBytes(certificate);
    log('Certificates found, using secure connection', level: LogLevel.info);
  } on PathNotFoundException catch (_) {
    log('No certificates found, using insecure connection',
        level: LogLevel.warning);
  }
  final server =
      _server = NetworkerSocketServer(InternetAddress.anyIPv4, port,
          securityContext: securityContext,
          filterConnections: buildFilterConnections(
              loadProperty: (request) => eventSystem.runPing(
                  request,
                  GameProperty.defaultProperty.copyWith(
                    description: description,
                    maxPlayers: maxPlayers,
                    currentPlayers: _server?.clientConnections.length,
                  ))));

  final transformer = _pipe = NetworkerPipeTransformer<String, WorldEvent>(
      WorldEventMapper.fromJson, (e) => e.toJson());
  transformer.read.listen(_onClientEvent);
  server
    ..clientConnect.listen(_onJoin)
    ..clientDisconnect.listen(_onLeave)
    ..connect(StringNetworkerPlugin()..connect(transformer));
  await _server?.init();

  consoler.registerProgram('stop', StopProgram(this));
  consoler.registerProgram('save', SaveProgram(this));
  consoler.registerProgram('packs', PacksProgram(this));
  consoler.registerProgram('players', PlayersProgram(this));
  consoler.registerProgram('say', SayProgram(this));
  consoler.registerProgram(null, UnknownProgram());
}