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}');
}
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:
Flutter: