Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router with GET in combination with websockets not working #394

Open
develocode777 opened this issue Nov 28, 2023 · 0 comments
Open

Router with GET in combination with websockets not working #394

develocode777 opened this issue Nov 28, 2023 · 0 comments

Comments

@develocode777
Copy link

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!'),
        ),
      ),
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant