dart-lang / shelf

Web server middleware for Dart
https://pub.dev/packages/shelf
BSD 3-Clause "New" or "Revised" License
893 stars 125 forks source link

Router with GET in combination with websockets not working #394

Open develocode777 opened 7 months ago

develocode777 commented 7 months ago

Hello,

When using get methods in combination with websockets the websocket connection fails with the message "[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: WebSocketChannelException: WebSocketChannelException: WebSocketException: Connection to 'http://localhost:8080#' was not upgraded to websocket". If I use post methods everything is ok.

Is this a bug or am I doing something wrong? This is the code I use:

Server:

import 'dart:io';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf_web_socket/shelf_web_socket.dart';

// Configure routes.
final _router = Router()
  ..get('/', _rootHandler)
  ..get('/echo/<message>', _echoHandler);

Response _rootHandler(Request req) {
  return Response.ok('Hello, World!\n');
}

Response _echoHandler(Request request) {
  final message = request.params['message'];
  return Response.ok('$message\n');
}

void main(List<String> args) async {
  // Use any available host or container IP (usually `0.0.0.0`).
  final ip = InternetAddress.anyIPv4;

  var ws = webSocketHandler((webSocket) {
    webSocket.stream.listen((message) {
      webSocket.sink.add("echo $message");
    });
  });

  // Configure a pipeline that logs requests.
  final handlerRouter =
      Pipeline().addMiddleware(logRequests()).addHandler(_router);
  final handlerWs = Pipeline().addMiddleware(logRequests()).addHandler(ws);

  final handler = Cascade().add(handlerRouter).add(handlerWs).handler;
  // For running in containers, we respect the PORT environment variable.
  final port = int.parse(Platform.environment['PORT'] ?? '8080');
  final server = await serve(handler, ip, port);
  print('Server listening on port ${server.port}');
}

Flutter:

import 'package:flutter/material.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/status.dart' as status;

void main() {
  runApp(const MainApp());
}

class MainApp extends StatefulWidget {
  const MainApp({super.key});

  @override
  State<MainApp> createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {
  var channel;
  @override
  void initState() {
    super.initState();
    final wsUrl = Uri.parse('ws://localhost:8080');
    channel = WebSocketChannel.connect(wsUrl);

    channel.stream.listen((message) {
      channel.sink.add('received!');
    });
  }

  @override
  void dispose() {
    channel.sink.close(status.goingAway);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}