I get the following error when attempting to invoke server side method from a javascript client via SignalR:
XMLHttpRequest cannot load http://localhost:49679/signalr/send?transport=serverSentEvents&clientProtoc…XWYPNm9Av7oD1DiEodZenKiQdpQFWUqDJ9vD0UgrYc%2FjZs21i3&connectionData=%5B%5D. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. The response had HTTP status code 500.
I am building a simple prototype and this is what I have in my Startup.cs class in my Web server:
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Owin;
[assembly: OwinStartupAttribute(typeof(WebRTCSignalServer.Web.Startup))]
namespace WebRTCSignalServer.Web
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
//app.UseCors(CorsOptions.AllowAll);
//Add SignalR
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
// configure signalR
HubConfiguration hubConfiguration = new HubConfiguration()
{
EnableJavaScriptProxies = false,
EnableDetailedErrors = true
};
//GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule());
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
ConfigureAuth(app);
}
}
}
This is the javascript signalr client code:
var connection = $.hubConnection('http://localhost:49679/signalr', { useDefaultPath: false });
console.log(connection);
connection.stateChanged(function () {
console.log('stateChanged.... state=' + connection.state);
});
connection.connectionSlow(function () {
console.log('connectionSlow.... state=' + connection.state);
});
connection.reconnecting(function () {
console.log('reconnecting.... state=' + connection.state);
});
connection.reconnected(function () {
console.log('reconnected.... state=' + connection.state);
});
connection.disconnected(function () {
console.log('disconnected.... state=' + connection.state);
});
connection.error(function (error) {
console.log('error.... state=' + connection.state);
});
var webRTCSignalServerHub = connection.createHubProxy('webrtcsignalserver');
console.log(webRTCSignalServerHub);
connection.start().done(function(){
webRTCSignalServerHub.invoke('register', 'wkst0314');
});
Can someone explain why this is happening and how to fix it please? I followed this example in the SignalR docs to enable CORS but it is not working for me. Not sure if it is important but I am trying this in Chrome: http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#crossdomain
Thanks in advance for the help.