getWebSocketUrl method

String getWebSocketUrl({
  1. required String url,
  2. required String protocol,
  3. required String version,
})

Get the websocket url based on a given url.

Implementation

String getWebSocketUrl({
  required String url,
  required String protocol,
  required String version,
}) {
  url = url.startsWith('https')
      ? url.replaceFirst('https', 'wss')
      : url.startsWith('http')
          ? url.replaceFirst('http', 'ws')
          : url;

  final splitUrl = url.split('?');

  final params = Uri.dataFromString(url).queryParameters;
  final queryParams = {
    ...params,
    'protocol': protocol,
    'version': version,
    'env': 'browser',
    'host': 'test',
  };
  final queryString = Uri(queryParameters: queryParams).query;
  return '${splitUrl[0]}?$queryString';
}