websocket connection bug fixed.

merge-AudioVideo-Copilot
Sikander Saleem 2 months ago
parent 13aa5a1258
commit 157d8138e3

@ -8,6 +8,7 @@ import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:signalr_netcore/http_connection_options.dart'; import 'package:signalr_netcore/http_connection_options.dart';
import 'package:signalr_netcore/hub_connection.dart'; import 'package:signalr_netcore/hub_connection.dart';
import 'package:signalr_netcore/hub_connection_builder.dart'; import 'package:signalr_netcore/hub_connection_builder.dart';
import 'package:signalr_netcore/itransport.dart';
import 'package:test_sa/controllers/api_routes/urls.dart'; import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/modules/cx_module/chat/api_client.dart'; import 'package:test_sa/modules/cx_module/chat/api_client.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
@ -70,8 +71,10 @@ class CallProvider with ChangeNotifier, DiagnosticableTreeMixin {
); );
final HttpConnectionOptions httpOp = HttpConnectionOptions( final HttpConnectionOptions httpOp = HttpConnectionOptions(
transport: HttpTransportType.LongPolling,
skipNegotiation: false, skipNegotiation: false,
logMessageContent: true, // Keep false: signalr_netcore 1.4.4 crashes when logging + LongPolling.
logMessageContent: false,
accessTokenFactory: () async => token, accessTokenFactory: () async => token,
requestTimeout: 30000, requestTimeout: 30000,
); );

@ -401,27 +401,18 @@ class ChatProvider with ChangeNotifier, DiagnosticableTreeMixin {
throw StateError('Cannot open SignalR hub: chat login token is null/empty. Call getUserAutoLoginTokenSilent() first.'); throw StateError('Cannot open SignalR hub: chat login token is null/empty. Call getUserAutoLoginTokenSilent() first.');
} }
// NOTE: build the url with Uri so every query value is properly encoded.
// Never hand-concatenate the JWT - unencoded characters silently break the
// negotiate request and the server answers 401 (which surfaces as the
// confusing "https://host:0/... was not upgraded to websocket" error,
// see comment on [buildHubConnection]).
final Uri hubUri = Uri.parse(URLs.chatHubUrlChat).replace(queryParameters: <String, String>{ final Uri hubUri = Uri.parse(URLs.chatHubUrlChat).replace(queryParameters: <String, String>{
"UserId": "${login?.userId}", "UserId": "${login?.userId}",
"source": "Desktop", "source": "Desktop",
"access_token": token, "access_token": token,
}); });
final String hubUrl = _urlWithExplicitPort(hubUri); final String hubUrl = _urlWithExplicitPort(hubUri);
// Send the token as a real `Authorization: Bearer` header too.
// signalr_netcore uses `accessTokenFactory` for BOTH the /negotiate call and
// the websocket handshake on mobile (query strings are only used on web),
// so without this the websocket upgrade request is unauthenticated -> 401.
final HttpConnectionOptions httpOp = HttpConnectionOptions( final HttpConnectionOptions httpOp = HttpConnectionOptions(
transport: HttpTransportType.LongPolling,
skipNegotiation: false, skipNegotiation: false,
logMessageContent: true, logMessageContent: false,
accessTokenFactory: () async => token, accessTokenFactory: () async => token,
requestTimeout: 30000, // package default is only 2000ms requestTimeout: 30000,
); );
final HubConnection hub = HubConnectionBuilder() final HubConnection hub = HubConnectionBuilder()

Loading…
Cancel
Save