Repository: flutter
Issue: Flutter run web on localhost with subdomain, dev.localhost:{port}/
Labels: c: new feature,tool,platform-web,has reproducible steps,P2,team-web,triaged-web,found in release: 3.19,found in release: 3.20

### Steps to reproduce

1. Run the project on web > flutter run -d chrome
2. The app opens in the browser, http://localhost:{port}
3. Edit the url in the browser and add subdomain, Example > http://dev.localhost:{port}

### Expected results

Expected the app to reload and work smoothly...

### Actual results

The app crashes and outputs the following Socket error:

```console
Finished with error: Failed to establish connection with the application instance in Chrome.
This can happen if the websocket connection used by the web tooling is unable to correctly establish a connection, for example due to a firewall.
```

The full error stack:
```console
WebSocketException: Connection to 'http://127.0.0.1:61965/mWYvPARGOJI=/ws#' was not upgraded to websocket
#0      _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1011:41)
#1      WebSocket.connect (dart:_http/websocket.dart:320:22)
#2      new IOWebSocketChannel.connect (package:web_socket_channel/io.dart:81:28)
#3      connect (package:web_socket_channel/src/_connect_io.dart:15:24)
#4      new WebSocketChannel.connect (package:web_socket_channel/src/channel.dart:144:16)
#5      _defaultWebSocketBuilder (package:dds/src/dds_impl.dart:52:27)
#6      DartDevelopmentServiceImpl.startService (package:dds/src/dds_impl.dart:80:40)
#7      DartDevelopmentService.startDartDevelopmentService (package:dds/dds.dart:97:19)
<asynchronous suspension>
#8      DebugService.startDartDevelopmentService (package:dwds/src/services/debug_service.dart:166:12)
<asynchronous suspension>
#9      DevHandler._createAppDebugServices (package:dwds/src/handlers/dev_handler.dart:511:7)
<asynchronous suspension>
#10     DevHandler.loadAppServices (package:dwds/src/handlers/dev_handler.dart:238:21)
<asynchronous suspension>
#11     Dwds.debugConnection (package:dwds/dart_web_debug_service.dart:58:30)
<asynchronous suspension>
#12     WebDevFS.connect.<anonymous closure> (package:flutter_tools/src/isolated/devfs_web.dart:723:15)
<asynchronous suspension>

Finished with error: Failed to establish connection with the application instance in Chrome.
This can happen if the websocket connection used by the web tooling is unable to correctly establish a connection, for example due to a firewall.
```

### Code sample

<details open><summary>Code sample</summary>
Basically any hello world sample code...

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: const Center(
        child: Text("Hello There..."),
      ),
    );
  }
}

```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
/Users/xxx/Documents/flutter/bin/flutter doctor --verbose
[✓] Flutter (Channel beta, 3.19.0-0.4.pre, on macOS 14.2.1 23C71 darwin-x64, locale en-DZ)
    • Flutter version 3.19.0-0.4.pre on channel beta at /Users/xxx/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b7e7d46a04 (3 weeks ago), 2024-02-02 08:21:06 -0600
    • Engine revision 98820f0a77
    • Dart version 3.3.0 (build 3.3.0-279.3.beta)
    • DevTools version 2.31.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/xxx/Library/Android/sdk
    • Platform android-34-ext8, build-tools 34.0.0
    • Java binary at: /Applications/Android/Android Studio Preview 2.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C500b
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (version unknown)
    • Android Studio at /Applications/Android/Android Studio Preview 2.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to determine Android Studio version.
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)

[!] Android Studio (version unknown)
    • Android Studio at /Applications/Android Studio Preview.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    ✗ Unable to determine Android Studio version.
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)

[✓] IntelliJ IDEA Ultimate Edition (version 2021.2.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] IntelliJ IDEA Ultimate Edition (version 2023.1.2)
    • IntelliJ at /Applications/IntelliJ IDEA 2.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.84.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Connected device (4 available)
    • Rabia’s iPhone (mobile)    • 00008030-00146458219B402E            • ios            • iOS 17.3.1 21D61
    • iPhone 15 Pro Max (mobile) • 417F9C51-A6EB-4A06-BAF8-E5C81EEA0973 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
    • macOS (desktop)            • macos                                • darwin-x64     • macOS 14.2.1 23C71 darwin-x64
    • Chrome (web)               • chrome                               • web-javascript • Google Chrome 121.0.6167.184

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.
Process finished with exit code 0
```

</details>