shelf icon indicating copy to clipboard operation
shelf copied to clipboard

WebSocketException: Connection to '${url}' was not upgraded to websocket

Open mengyanshou opened this issue 2 years ago • 1 comments

I know from https://github.com/dart-lang/web_socket_channel/issues/164#issuecomment-1026354706 dart will convert all request header key to lowercase. and in this repo,we can see some code like this:

    final connection = request.headers['Connection'];
    if (connection == null) return _notFound();
    final tokens =
        connection.toLowerCase().split(',').map((token) => token.trim());
    if (!tokens.contains('upgrade')) return _notFound();

    final upgrade = request.headers['Upgrade'];
    if (upgrade == null) return _notFound();
    if (upgrade.toLowerCase() != 'websocket') return _notFound();

and my flutter app for mac desktop in debug mode cannot connect this websocket powered by webSocketHandler,then i change webSocketHandler key to lowercase,like this:

    final connection = request.headers['Connection'];
    if (connection == null) return _notFound();
    final tokens =
        connection.toLowerCase().split(',').map((token) => token.trim());
    if (!tokens.contains('upgrade')) return _notFound();

    final upgrade = request.headers['upgrade'];
    if (upgrade == null) return _notFound();
    if (upgrade.toLowerCase() != 'websocket') return _notFound();

everything is working properly. oddly enough, it all worked in Android,web, and release mode on mac without any changes

mengyanshou avatar Sep 08 '22 07:09 mengyanshou

Had some strange behavior with this as well, we have many websockets in our application that have been functioning well for over a year or two now, had a more complex socket case that should have functioned no problem(worked well in debug && in release mode, but failed in our actual production application).

I come to find out that something with the --obfuscate flag that messed with the request up giving back the same clientside error

mark-at-pieces avatar Aug 22 '23 22:08 mark-at-pieces