sefidgaran / signalr_client

A Flutter SignalR Client for ASP.NET Core
https://pub.dev/packages/signalr_netcore
MIT License
78 stars 112 forks source link

HttpConnection.stopConnection(Unknown) called while in state ConnectionState.Connected. #45

Open MeiChao opened 1 year ago

MeiChao commented 1 year ago

Launching lib/main.dart on HMA AL00 in debug mode... Running Gradle task 'assembleDebug'... ✓ Built build/app/outputs/flutter-apk/app-debug.apk. Installing build/app/outputs/flutter-apk/app.apk... Debug service listening on ws://127.0.0.1:56868/ZJDHRABsZoM=/ws Syncing files to device HMA AL00... I/flutter (14829): Starting HubConnection. I/flutter (14829): Starting connection with transfer format 'TransferFormat.Text'. I/flutter (14829): Sending negotiation request: http://api.dev.leshare.com/signalr-hubs/real-time/negotiate?negotiateVersion=1 W/Gralloc3(14829): mapper 3.x is not supported I/flutter (14829): HTTP send: url 'http://api.dev.leshare.com/signalr-hubs/real-time/negotiate?negotiateVersion=1', method: 'POST' content: '' content length = '0' headers: '{ content-type: text/plain;charset=UTF-8 }, { X-Requested-With: FlutterHttpClient }, { Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IjQyM0E1Q0ZEMDFGRDU5MkFGNjc2RTRGMDAwOTY0NDA4IiwidHlwIjoiYXQrand0In0.eyJuYmYiOjE2NzUzMTgyNDMsImV4cCI6MTcwNjg1NDI0MywiaXNzIjoiaHR0cDovL2FwaS5kZXYubGVzaGFyZS5jb20iLCJhdWQiOiJFTUMiLCJjbGllbnRfaWQiOiJFTUNfQXBwIiwic3ViIjoiM2JhODI1ODQtNzM4Ny02MmQyLTk1NzYtM2EwM2VjNjI2NDI4IiwiYXV0aF90aW1lIjoxNjc1MzE4MjQyLCJpZHAiOiJsb2NhbCIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL2dpdmVubmFtZSI6IuWImOa4ryIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3N1cm5hbWUiOiLlmLvlmLvlmLvmg7PnnIvnnIsiLCJwaG9uZV9udW1iZXIiOiIxMzY3MDExOTA4NiIsInBob25lX251bWJlcl92ZXJpZmllZCI6IlRydWUiLCJlbWFpbCI6Inpob3VoYWltaW5nQGxleGlheXVuLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjoiVHJ1ZSIsIm5hbWUiOiJkZGRhZG1pbiIsImlhdCI6MTY3NTMxODI I/HwViewRootImpl(14829): removeInvalidNode jank list is null I/flutter (14829): Selecting transport 'HttpTransportType.WebSockets'. I/flutter (14829): (WebSockets transport) Connecting I/flutter (14829): WebSocket try connecting to 'ws://api.dev.leshare.com/signalr-hubs/real-time?id=L0ebZWKChkZhg6dyudoY_Q'. I/flutter (14829): WebSocket connected to 'ws://api.dev.leshare.com/signalr-hubs/real-time?id=L0ebZWKChkZhg6dyudoY_Q'. I/flutter (14829): The HttpConnection connected successfully. I/flutter (14829): Sending handshake request. I/flutter (14829): (WebSockets transport) sending data. String data of length 32. Content: '{"protocol":"json","version":1}'. I/flutter (14829): Using HubProtocol 'json'. E/flutter (14829): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: WebSocketException: Connection to 'http://api.dev.leshare.com:0/signalr-hubs/real-time?id=L0ebZWKChkZhg6dyudoY_Q#' was not upgraded to websocket E/flutter (14829): #0 _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1011:41) E/flutter (14829): #1 WebSocket.connect (dart:_http/websocket.dart:360:22) E/flutter (14829): #2 new IOWebSocketChannel.connect (package:web_socket_channel/io.dart:80:28) E/flutter (14829): #3 connect (package:web_socket_channel/src/_connect_io.dart:15:24) E/flutter (14829): #4 new WebSocketChannel.connect (package:web_socket_channel/src/channel.dart:115:16) E/flutter (14829): #5 WebSocketTransport.connect (package:signalr_netcore/web_socket_transport.dart:53:35) E/flutter (14829): #6 HttpConnection._startTransport (package:signalr_netcore/http_connection.dart:574:24) E/flutter (14829): #7 HttpConnection._createTransport (package:signalr_netcore/http_connection.dart:529:15) E/flutter (14829): #8 HttpConnection._startInternal (package:signalr_netcore/http_connection.dart:409:15) E/flutter (14829): E/flutter (14829): #9 HttpConnection.start (package:signalr_netcore/http_connection.dart:255:5) E/flutter (14829): E/flutter (14829): #10 HubConnection._startInternal (package:signalr_netcore/hub_connection.dart:190:5) E/flutter (14829): E/flutter (14829): #11 HubConnection._startWithStateTransitions (package:signalr_netcore/hub_connection.dart:172:7) E/flutter (14829): E/flutter (14829): #12 ChatPageViewModel.openChatConnection (package:chatclient/chatPageViewModel.dart:113:7) E/flutter (14829): E/flutter (14829): I/flutter (14829): HttpConnection.stopConnection(Unknown) called while in state ConnectionState.Connected. I/flutter (14829): Connection disconnected. I/flutter (14829): HubConnection.connectionClosed(null) called while in state HubConnectionState.Connecting. I/flutter (14829): Hub handshake failed with error 'The underlying connection was closed before the hub handshake could complete.' during start(). Stopping HubConnection. I/flutter (14829): Call to HttpConnection.stop(Exception: The underlying connection was closed before the hub handshake could complete.) ignored because the connection is already in the disconnected state. I/flutter (14829): HubConnection failed to start successfully because of error 'The underlying connection was closed before the hub handshake could complete.'. E/flutter (14829): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: The underlying connection was closed before the hub handshake could complete. E/flutter (14829):

ChasakisD commented 1 year ago

@MeiChao Did you figured out a solution for this?

MeiChao commented 1 year ago

@MeiChao Did you figured out a solution for this?

Hello, there is no solution. We have currently replaced it with a new plugin.

ChasakisD commented 1 year ago

What plugin?

Also I figured out the solution The problem is that it gets 401 when trying to connect to the websocket after the negotiation The flutter sdk adds the bearer token as a query parameter. So adding the following code in the dotnet side, works like a charm

OnMessageReceived = ctx =>
{
    if (ctx.HttpContext.Request.Path.StartsWithSegments("/hubs"))
    {
        ctx.Token = ctx.Request.Query["access_token"];
    }
    return Task.CompletedTask;
},
MeiChao commented 1 year ago

Thank you for your reply. I will debug it later.

Skrface commented 1 year ago

Thank you very much for your solution @ChasakisD.

@sefidgaran Is it planned to pass autorisation code in headers rather than in the query in the future ? Btw thank you for your work on this awesome repo !

0xdanny commented 1 year ago

@sefidgaran Hi! Is there a way we could pass the Authorization token in the header rather than in the query parameters?