Cross-domain requests not working in SignalR 2.0.0-rc1

I recently updated the project from SignalR 2.0.0-beta1 to 2.0.0-rc1. I understand that cross-domain request support configuration has changed in RC1. I updated my project to use the new syntax, but now I get the following error when trying to contact my center:

XMLHttpRequest cannot load = 1377623738064 "> HTTP: // local: 8080 / negotiates connectionData =% 5B% 7B% 22name% 22% 3A% 22chathub% 22% 7D% 5D & ClientProtocol = 1.3 & = 1377623738064 ?. Origin http://localhost:7176 not allowed Access-Control-Allow-Origin.

The client site runs at http://localhost:7176 , and the hub listens on the console application at http://localhost:8080 . Am I missing something? Cross domain requests worked before my upgrade to RC1.

CONSOLE APP ENTRANCE POINT

 static void Main(string[] args) { var chatServer = new ChatServer(); string endpoint = "http://localhost:8080"; chatServer.Start(endpoint); Console.WriteLine("Chat server listening at {0}...", endpoint); Console.ReadLine(); } 

CHATSERVER CLASS

 public class ChatServer { public IDisposable Start(string url) { return WebApp.Start<Startup>(url); } } 

STARTUP CONFIGURATION

 public class Startup { public void Configuration(IAppBuilder app) { app.Map("/signalr", map => { map.UseCors(CorsOptions.AllowAll); map.RunSignalR(new HubConfiguration { EnableJSONP = true }); }); } } 
+7
source share
4 answers

Something is wrong with your client configuration.

XMLHttpRequest cannot load =1377623738064">http://localhost:8080/negotiate?connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&clientProtocol=1.3&=1377623738064. Origin http://localhost:7176 is not allowed by Access-Control-Allow-Origin.

A negotiation request must be made http://localhost:8080/signalr/negotiate?... not http://localhost:8080/negotiate?... To fix this, you can try the following before you call $ .connection.hub.start:

$.connection.hub.url = http://localhost:8080/signalr;

+8
source

Not sure if this question was answered adequately, but I made the following changes to the example provided by Microsoft:

 public void Configuration(IAppBuilder app) { var config = new HubConfiguration(); config.EnableJSONP = true; app.MapSignalR(config); } 

And I added the following JS example:

 $.connection.hub.start({ jsonp: true }).done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#displayname').val(), $('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); 

And now the Cross domain script is enabled. Hope this helps someone else, I really puzzled him for a while.

+8
source

For Microsoft.Owin 2.x and higher:

Add the Microsoft.Owin.Cors package through NuGet using this command in the package manager console:

 PM> Install-Package Microsoft.Owin.Cors 

and then using this package in the Startup class file:

 using Microsoft.Owin; using Microsoft.Owin.Cors; 

then change your source code as follows:

 // app.MapHubs(new HubConfiguration { EnableCrossDomain = true }); app.UseCors(CorsOptions.AllowAll); app.MapSignalR(); 
+5
source

I think the best way to deal with Cross Domain is documented here CrossDomain R Signal

+2
source

Source: https://habr.com/ru/post/952611/


All Articles