vmServiceUriToWebSocket method

Uri vmServiceUriToWebSocket(
  1. Uri uri
)

Fixes up a VM Service uri to a WebSocket URI with a trailing /ws for connecting when not using DDS.

DDS does its own cleaning up of the URI.

Implementation

Uri vmServiceUriToWebSocket(Uri uri) {
  // The VM Service library always expects the WebSockets URI so fix the
  // scheme (http -> ws, https -> wss).
  final isSecure = uri.isScheme('https') || uri.isScheme('wss');
  uri = uri.replace(scheme: isSecure ? 'wss' : 'ws');

  if (uri.path.endsWith('/ws') || uri.path.endsWith('/ws/')) {
    return uri;
  }

  final append = uri.path.endsWith('/') ? 'ws' : '/ws';
  final newPath = '${uri.path}$append';
  return uri.replace(path: newPath);
}