connect method

void connect({
  1. String? url,
  2. String? path,
  3. bool requiresAuthorization = true,
})

Connect to the socket endpoint in the server.

If the socket is already connected, the socket will be disconnected before reconnecting.

By default requiresAuthorization is set to true which adds "Authorization" headers based on RenovationRequestOptions.

Implementation

void connect({String? url, String? path, bool requiresAuthorization = true}) {
  url ??= config.hostUrl;
  path ??= '/socket.io';

  final opts = OptionBuilder()
      .setPath(path)
      .setTransports(['websocket'])
      .setExtraHeaders(requiresAuthorization
          ? <String, dynamic>{
              'Authorization':
                  RenovationRequestOptions.headers!['Authorization']
            }
          : <String, dynamic>{})
      .build();

  if (_socket != null) {
    // Update the options
    // Need to reuse the same object created earlier
    _socket!.io
      ..uri = url
      ..options = opts;
    (_socket?.disconnect())!.connect();
  } else {
    _socket = io(url, opts);

    _socket?.onConnect((dynamic data) =>
        config.logger.i('Connected socket successfully on $url$path'));

    _socket?.onConnectError((dynamic connectStatus) =>
        config.logger.e('Connected socket unsuccessful on $url$path'));

    _socket?.onConnectTimeout((dynamic connectStatus) => config.logger
        .e(<dynamic>['Timeout while connecting', connectStatus]));
  }

  config.logger.i('LTS-Renovation-Core-Dart Connecting socket on $url$path');
}