Connect to a signal node

I created a simple SignalR console inside a console application:

class Program { static void Main(string[] args) { using (WebApp.Start<Startup>("http://localhost:1968")) { Console.WriteLine("Server running!"); Console.ReadLine(); } } } public static class UserHandler { public static HashSet<string> ConnectedIds = new HashSet<string>(); } [HubName("echo")] public class EchoHub : Hub { public void Say(string message) { Trace.WriteLine("hub: "+message); Clients.All.AddMessage(message); } public override Task OnConnected() { UserHandler.ConnectedIds.Add(Context.ConnectionId); return base.OnConnected(); } public override Task OnDisconnected() { UserHandler.ConnectedIds.Remove(Context.ConnectionId); return base.OnDisconnected(); } } class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } 

When I try to connect this Silverlight application, it succeeds:

 static Microsoft.AspNet.SignalR.Client.HubConnection signalR { get; set; } public static Microsoft.AspNet.SignalR.Client.IHubProxy signalRhub { get; set; } public static void StartSignalR() { var url = "http://localhost:1968"; signalR = new Microsoft.AspNet.SignalR.Client.HubConnection(url); signalR.Received += signalR_Received; signalRhub = signalR.CreateHubProxy("echo"); signalR.Start().Wait(); signalRhub.Invoke("Say", "hub invoked"); } 

My next step is to connect the SignalR hub from jquery:

 <script src="../Scripts/jquery-1.6.4.js"></script> <script src="../Scripts/jquery.signalR-2.1.0.js"></script> <script> $(function () { var connection = $.hubConnection("http://localhost:1968"); connection.start() .done(function () { console.log('connected'); connection.send("success?"); }) .fail(function (a) { console.log('not connected'+a); }); }); </script> 

When I try to run this script, it gives an error:

 "XMLHttpRequest cannot load localhost:1968/signalr/negotiate?clientProtocol=1.4&_=1404978593482. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin <code>http://localhost:55282</code> is therefore not allowed access." 

Why can I connect to the hub from my Silverlight page (hosted at localhost: 3926)
and it fails when I run the jquery script (located in localhost: 55282)?

What can I do to get a working connection between my jQuery and SignalR hub?

+6
source share
2 answers

Change

 $(function () { var connection = $.hubConnection("http://localhost:1968"); connection.start() .done(function () { console.log('connected'); connection.send("success?"); }) .fail(function (a) { console.log('not connected'+a); }); }); 

to

 $(function () { var connection = $.hubConnection("http://localhost:1968"); var hub = connection.createHubProxy("echo"); hub.on("AddMessage", Method); connection.start({ jsonp: true }) .done(function () { console.log('connected'); hub.say("success?"); }) .fail(function (a) { console.log('not connected'+a); }); }); function Method(messageFromHub) { alert(messageFromHub); } 

and

 class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } 

to

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

He has to do.

 app.MapSignalR(new HubConfiguration() { EnableJSONP = true });} 

and

 connection.start({ jsonp: true }) 

Allows cross site request

+13
source

RPC on the server in SignalR with createHubProxy ():

Thanks to the answer of Vishal Ravlani

But for me

 hub.say("success?"); 

does not work! (Does it work for you?)

I need to write:

 hub.invoke('say','success?'); 

And SignalR automatically detects CrossOrigin on the client.

On the server, I used:

 app.Map("/signalr", map => { map.UseCors(CorsOptions.AllowAll); map.RunSignalR(); }); 

which was described at: http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client#crossdomain

+1
source

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


All Articles