sefidgaran / signalr_client

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

Various error messages, no proposed solution works #78

Closed ktkk closed 6 months ago

ktkk commented 6 months ago

When trying to connect to my SignalR server (running locally) using a flutter app running on Windows, I'm getting various errors. Almost all of these errors appear in other issues on this repo, but none of the solutions proposed in these issues seem to help.

The messages in question: (access token replaced by <my-token-omitted>)

[SignalR - Hub] FINER: Starting HubConnection.
[SignalR - Hub] Starting HubConnection.
[SignalR - Transport] FINER: Starting connection with transfer format 'TransferFormat.Text'.
[SignalR - Transport] Starting connection with transfer format 'TransferFormat.Text'.
[SignalR - Transport] FINER: Sending negotiation request: https://localhost:7167/chat/negotiate?negotiateVersion=1
[SignalR - Transport] Sending negotiation request: https://localhost:7167/chat/negotiate?negotiateVersion=1
[SignalR - Transport] FINEST: HTTP send: url 'https://localhost:7167/chat/negotiate?negotiateVersion=1', method: 'POST' content: '' content length = '0' headers: '{ content-type: text/plain;charset=UTF-8 }, { X-Requested-With: FlutterHttpClient }, { Authorization: Bearer <my-token-omitted> }'
[SignalR - Transport] HTTP send: url 'https://localhost:7167/chat/negotiate?negotiateVersion=1', method: 'POST' content: '' content length = '0' headers: '{ content-type: text/plain;charset=UTF-8 }, { X-Requested-With: FlutterHttpClient }, { Authorization: Bearer <my-token-omitted> }'
[SignalR - Transport] FINER: Selecting transport 'HttpTransportType.WebSockets'.
[SignalR - Transport] Selecting transport 'HttpTransportType.WebSockets'.
[SignalR - Transport] FINEST: (WebSockets transport) Connecting
[SignalR - Transport] (WebSockets transport) Connecting
[SignalR - Transport] FINEST: WebSocket try connecting to 'wss://localhost:7167/chat?id=-lbtKuL3e1JNZWYmwvUZwA&access_token=<my-token-omitted>'.
[SignalR - Transport] WebSocket try connecting to 'wss://localhost:7167/chat?id=-lbtKuL3e1JNZWYmwvUZwA&access_token=<my-token-omitted>'.
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: WebSocketException: Connection to 'https://localhost:7167/chat?id=-lbtKuL3e1JNZWYmwvUZwA&access_token=<my-token-omitted>#' was not upgraded to websocket
#0      _WebSocketImpl.connect (dart:_http/websocket_impl.dart:1011:41)
#1      WebSocket.connect (dart:_http/websocket.dart:320:22)
#2      new IOWebSocketChannel.connect (package:web_socket_channel/io.dart:81:28)
#3      connect (package:web_socket_channel/src/_connect_io.dart:15:24)
#4      new WebSocketChannel.connect (package:web_socket_channel/src/channel.dart:115:16)
#5      WebSocketTransport.connect (package:signalr_netcore/web_socket_transport.dart:53:35)
<asynchronous suspension>
#6      HttpConnection._createTransport (package:signalr_netcore/http_connection.dart:529:9)
<asynchronous suspension>
#7      HttpConnection._startInternal (package:signalr_netcore/http_connection.dart:409:9)
<asynchronous suspension>
#8      HttpConnection.start (package:signalr_netcore/http_connection.dart:255:5)
<asynchronous suspension>
[SignalR - Transport] #9      HubConnection._startInternal (package:signalr_netcore/hub_connection.dart:222:5)
INFO: WebSocket connected to 'wss://localhost:7167/chat?id=-lbtKuL3e1JNZWYmwvUZwA&access_token=<my-token-omitted>INFO: WebSocket connected to 'wss://localhost:7167/chat?id=-lbtKuL3e1JNZWYmwvUZwA&access_token=<my-token-omitted>'.
#10     HubConnection._startWithStateTransitions (package:signalr_netcore/hub_connection.dart:204:7)
<asynchronous suspension>
#11     _ConversationPageState._initHubConnection (package:sitra_drivers/ui/pages/messaging/conversation/conversation_page.dart:79:9)
<asynchronous suspension>

[SignalR - Transport] WebSocket connected to 'wss://localhost:7167/chat?id=-lbtKuL3e1JNZWYmwvUZwA&access_token=<my-token-omitted>'.
[SignalR - Transport] FINER: The HttpConnection connected successfully.
[SignalR - Transport] The HttpConnection connected successfully.
[SignalR - Hub] FINER: Sending handshake request.
[SignalR - Hub] Sending handshake request.
[SignalR - Transport] FINEST: (WebSockets transport) sending data. String data of length 32. Content: '{"protocol":"json","version":1}'.
[SignalR - Transport] (WebSockets transport) sending data. String data of length 32. Content: '{"protocol":"json","version":1}'.
[SignalR - Hub] INFO: Using HubProtocol 'json'.
[SignalR - Hub] Using HubProtocol 'json'.
[SignalR - Transport] FINER: HttpConnection.stopConnection(Unknown) called while in state ConnectionState.Connected.
[SignalR - Transport] HttpConnection.stopConnection(Unknown) called while in state ConnectionState.Connected.
[SignalR - Transport] INFO: Connection disconnected.
[SignalR - Transport] Connection disconnected.
[SignalR - Hub] FINER: HubConnection.connectionClosed(null) called while in state HubConnectionState.Connecting.
[SignalR - Hub] HubConnection.connectionClosed(null) called while in state HubConnectionState.Connecting.
[SignalR - Hub] FINER: Hub handshake failed with error 'The underlying connection was closed before the hub handshake could complete.' during start(). Stopping HubConnection.
[SignalR - Hub] Hub handshake failed with error 'The underlying connection was closed before the hub handshake could complete.' during start(). Stopping HubConnection.
[SignalR - Transport] FINER: 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.
[SignalR - Transport] 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.
[SignalR - Hub] FINER: HubConnection failed to start successfully because of error 'The underlying connection was closed before the hub handshake could complete.'.
[SignalR - Hub] HubConnection failed to start successfully because of error 'The underlying connection was closed before the hub handshake could complete.'.

My hub connection setup:

// in `initState`

final hubConnectionOptions = HttpConnectionOptions(
  logger: Logger('SignalR - Transport'),
  logMessageContent: true,
  accessTokenFactory: () async {
    final oidcService = context.read<OidcService>();
    return oidcService.accessToken!;
  },
);
final builder = HubConnectionBuilder()
  ..configureLogging(Logger('SignalR - Hub'))
  ..withAutomaticReconnect()
  ..withUrl(
    'https://localhost:7167/chat',
    options: hubConnectionOptions,
  );

_hubConnection = builder.build();

if (_hubConnection.state != HubConnectionState.Connected) {
  try {
    await _hubConnection.start();
  } on Exception catch (e) {
    // ...
  }
}

Any help would be greatly appreciated. I've been struggling with this for days 😅.

ktkk commented 6 months ago

I've figured out what went wrong.

While in the process of designing my API, I had created a controller I had planned to use to request chat history etc. I called it ChatController and decorated it with the [Route("[controller]")] attribute, and then promptly forgot about it and started working on the SignalR part.

That controller, of course, conflicts with the hub endpoint I've been trying to connect to this whole time.

🤦