jamiewest / signalr_core

ASP.NET Core SignalR Dart Client
https://pub.dev/packages/signalr_core
MIT License
91 stars 58 forks source link

[Web] Failed to complete negotiation with the server: XMLHttpRequest error. #27

Closed AmbujaAK closed 7 months ago

AmbujaAK commented 3 years ago

version : signalr_core: ^1.0.5 code :


final connection = HubConnectionBuilder()
.withUrl(
"$kBaseUrl/serverpush",
HttpConnectionOptions(
logging: (level, message) => print('$level: $message'),
))
.build();
await connection.start();

connection.on('FlutterRecMsgFromHub', (message) {
  print(message.toString());
});


> *Issues :* 
Restarted application in 670ms.
LogLevel.debug: Starting HubConnection.
LogLevel.debug: Starting connection with transfer format 'TransferFormat.text'.
LogLevel.debug: Sending negotiation request: https://p4infrdevsignalrwebapp01.azurewebsites.net/serverpush/negotiate.
LogLevel.error: Failed to complete negotiation with the server: XMLHttpRequest error.
LogLevel.error: Failed to start the connection: XMLHttpRequest error.
LogLevel.debug: HubConnection failed to start successfully because of error '{XMLHttpRequest error..toString}'.
Error: XMLHttpRequest error.
    at Object.createErrorWithStack (http://localhost:63192/dart_sdk.js:4362:12)
    at Object._rethrow (http://localhost:63192/dart_sdk.js:38245:16)
    at async._AsyncCallbackEntry.new.callback (http://localhost:63192/dart_sdk.js:38239:13)
    at Object._microtaskLoop (http://localhost:63192/dart_sdk.js:38071:13)
    at _startMicrotaskLoop (http://localhost:63192/dart_sdk.js:38077:13)
    at http://localhost:63192/dart_sdk.js:33574:9
AdrianKlm commented 3 years ago

I have the same problem. Any information on when it will be fixed?

Jddl commented 3 years ago
            services.AddCors(op =>
            {
                op.AddPolicy("SignalRCors", set =>
                {
                    set.SetIsOriginAllowed(origin => true)
                       .AllowAnyHeader()
                       .AllowAnyMethod()
                       .AllowCredentials();
                });
            });
laimonas commented 3 years ago

@AmbujaAK maybe try to look there https://github.com/jamiewest/signalr_core/issues/24#issuecomment-710113013

agwanyaseen commented 3 years ago

Same issue, these plugin not working on flutter web , Any Idea till when it will going to be fixed?

milodude commented 3 years ago

The XMLHttpRequest error is due to Cors policy issue. I managed to solve it by doing the next steps in a C# application.

1- Define in your startup.cs file a new porperty ie:
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

2-At your configure method in the same Startup.cs file add the next line to add Cors services.AddCors(options => { options.AddPolicy(MyAllowSpecificOrigins, policy => { policy.WithOrigins("http://localhost:58944") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); Where WithOrigins must be filled with the UI host url you are trying to send the message from.

3- Use Cors at your Configure method in the same Startup.cs file app.UseCors(MyAllowSpecificOrigins); app.UseRouting(); It must be defined before UseRouting method.

Please give it a shot and let everyone know if this worked out for you.

petsve commented 2 years ago

Allowing Cors solved it for me too. But the port could change between runs so I made the server code a bit more generic. In .NET you could use the following:

services.AddCors(options => { options.AddPolicy(MyAllowSpecificOrigins, builder => builder.SetIsOriginAllowed(origin => new Uri(origin).IsLoopback) ); });

Codzaa commented 2 years ago

@jamiewest Hi !

I bumped into this same error but I found a different solution that does not involve configuring 'CORS'.

final connection = HubConnectionBuilder() .withUrl( 'http://localhost:9670/hub', HttpConnectionOptions( skipNegotiation: true, transport: HttpTransportType.webSockets, logging: (level, message) => print(message), )) .build();

You can simply set skipNegotiation to true, but when reading the docs on the microsoft site (https://docs.microsoft.com/en-us/javascript/api/%40aspnet/signalr/ihttpconnectionoptions?view=signalr-js-latest#skipnegotiation) , it says that you have to be using Web Sockets for this to work.

By the way what are the downsides to setting skipNegotiation to true, any security issues or something?

Environment Flutter 2.8.0 .Net 5