convertToWebSocketUrl function Null safety

Uri convertToWebSocketUrl(
  1. {required Uri serviceProtocolUrl}
)

Map the URI to a WebSocket URI for the VM service protocol.

If the URI is already a VM Service WebSocket URI it will not be modified.

Implementation

Uri convertToWebSocketUrl({required Uri serviceProtocolUrl}) {
  final isSecure = serviceProtocolUrl.isScheme('wss') ||
      serviceProtocolUrl.isScheme('https');
  final scheme = isSecure ? 'wss' : 'ws';

  final path = serviceProtocolUrl.path.endsWith('/ws')
      ? serviceProtocolUrl.path
      : (serviceProtocolUrl.path.endsWith('/')
          ? '${serviceProtocolUrl.path}ws'
          : '${serviceProtocolUrl.path}/ws');

  return serviceProtocolUrl.replace(scheme: scheme, path: path);
}