hosseinmd / react-signalr

MIT License
43 stars 14 forks source link

Can't connect #29

Closed Clamb94 closed 1 year ago

Clamb94 commented 1 year ago

Hi, I'm having a issue that I can't connect to my signalR server. I'm using Electron with react (in case that matters), have installed react-signalr via npm and then downgraded the micoroft/signalr library down to version 6.0.21, as I've seen in the other issue that 7.x causes issues. Like this:

npm install react-signalr
npm remove @microsoft/signalr
npm install @microsoft/signalr@6.0.21

When I try to connect to my asp.net server, I'm getting the following errors:

Access to fetch at 'http://localhost:7573/electronHub/negotiate?negotiateVersion=1' from origin 'http://localhost:1212' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

FetchHttpClient.js:70 POST http://localhost:7573/electronHub/negotiate?negotiateVersion=1 net::ERR_FAILED Error: Failed to complete negotiation with the server: TypeError: Failed to fetch Error: Failed to start the connection: Error: Failed to complete negotiation with the server: TypeError: Failed to fetch

Error: Failed to complete negotiation with the server: TypeError: Failed to fetch
    at HttpConnection._getNegotiationResponse (HttpConnection.js:255:1)
    at async HttpConnection._startInternal (HttpConnection.js:168:1)
    at async HttpConnection.start (HttpConnection.js:72:1)
    at async HubConnection._startInternal (HubConnection.js:128:1)
    at async HubConnection._startWithStateTransitions (HubConnection.js:105:1)
    at async checkForStart (providerNativeFactory.js:33:1)

My TS code looks like this:

import { createSignalRContext } from 'react-signalr';

(...)

const SignalRContext = createSignalRContext();
export default function Layout() {
  return (
<SignalRContext.Provider url={'http://localhost:7573/electronHub'}>
  (...)
</SignalRContext.Provider>
)}

I've enabled CORS on the asp.net server like this:

            builder.Services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy", builder => builder.WithOrigins(origins)
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials()
                    .SetIsOriginAllowed((host) => true));
            });

Any idea how to solve this? SignalR in general is working fine. With files directly hosted on the server, I am able to connect.

Thanks :) Best regards Axel

hosseinmd commented 1 year ago

This origin 'http://localhost:1212/' is not allowed by your server, when you enable cors it will block requests from other origins. However, it is not related to this package.