Peer constructor

Peer({
  1. String? id,
  2. PeerOptions? options,
})

Implementation

Peer({String? id, PeerOptions? options}) {
  String? userId = id;

  PeerOptions initOptions = PeerOptions(
      debug: LogLevel.Disabled,
      host: PeerConfig.CLOUD_HOST,
      port: PeerConfig.CLOUD_PORT,
      path: "/",
      key: _defaultKey,
      token: util.randomToken(),
      config: PeerConfig.defaultConfig);

  if (options != null) {
    initOptions = initOptions.merge(options);
  }

  _options = initOptions;

  // Set path correctly.
  if (_options.path != null) {
    if (_options.path![0] != "/") {
      _options.path = "/${_options.path!}";
    }

    if (_options.path![_options.path!.length - 1] != "/") {
      _options.path = "${_options.path!}/";
    }
  }

  // Set a custom log function if present
  if (_options.logFunction != null) {
    logger.setLogFunction(_options.logFunction!);
  }

  logger.logLevel = this.options.debug ?? LogLevel.Disabled;

  _api = API(options: _options);
  _socket = _createServerConnection();

  if (userId != null) {
    _initialize(userId);
  } else {
    _api
        .retrieveId()
        .then((value) => _initialize(value))
        .catchError((error) => _abort(PeerErrorType.ServerError, error));
  }
}