locked
SignalR negotiate succesfully but fails start request RRS feed

  • Question

  • User1020904144 posted

    I am developing a chat application for our internal application using SignalR in a javascript angularJS client with a (self hosted for the moment) webAPI. This is in a cross domain connection.

    My problem is whenever I try to establish a connexion with my hub,

    [08:26:38 GMT-0400 (Est (heure d’été))] SignalR: Auto detected cross domain url.jquery.signalR.js:82 
    [08:26:38 GMT-0400 (Est (heure d’été))] SignalR: Client subscribed to hub 'chathub'.jquery.signalR.js:82
    [08:26:38 GMT-0400 (Est (heure d’été))] SignalR: Negotiating with 'https: localhost:44361/signalr/negotiateclientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D'.jquery.signalR.js:82 
    [08:26:38 GMT-0400 (Est (heure d’été))] SignalR: webSockets transport starting.jquery.signalR.js:82 
    [08:26:38 GMT-0400 (Est (heure d’été))] SignalR: Connecting to websocket endpoint 'wss: localhost:44361/signalr/connect?transport=webSockets&clientProtocol=1…kAIY9w9Q%3D%3D&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&tid=4'.jquery.signalR.js:82 
    [08:26:38 GMT-0400 (Est (heure d’été))] SignalR: Websocket opened.

    the start request fails

    [08:26:38 GMT-0400 (Est (heure d’été))] SignalR: webSockets transport connected. Initiating start request.
    Failed to load resource: the server responded with a status of 500 ()
    XMLHttpRequest cannot load https: localhost:44361/signalr/start?transport=webSockets&clientProtocol=1…D%3D&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&_=1471436795468. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https: localhost:3000' is therefore not allowed access. The response had HTTP status code 500.

    I've tried to pin point this problem for a couple of days now and what've noticed is that in the start request call, the response is missing the 'Access-Control-Allow-Origin' header. What bugs me most is that the negotiate request and the abort request both contains the header

    _Negotiate Request_

    Request URL:https: localhost:44361/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&_=14714 39245326
    Request Method:GET
    Status Code:200 OK
    Remote Address:[::1]:44361
    

    _Response Headers_

    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Origin:https: localhost:3000
    Cache-Control:no-cache
    Content-Type:application/json; charset=UTF-8
    Date:Wed, 17 Aug 2016 13:07:29 GMT
    Expires:-1
    Pragma:no-cache
    Server:Microsoft-IIS/10.0
    Transfer-Encoding:chunked
    X-AspNet-Version:4.0.30319
    X-Content-Type-Options:nosniff
    X-Powered-By:ASP.NET
    X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNccmFwaGFlbC5tb3JpblxTb3VyY2VcUmVwb3NcVGVhbXdvcmtTb2x1dGlvblxUZWFtd29yay5BcGlcc2lnbmFsclxuZWdvdGlhdGU=?=

    but not my start request

    _Start Request_

    Request URL:https: localhost:44361/signalr/start?transport=webSockets&clientProtocol=1.5&connectionToken=tR9V6HAxpgmW7r5Ro%2BzJzhUoJdMUcmv7eDv1ZDM%2Fq6yur21LXCZ2Dg1rrNrDGc5VBXQzfanyisyZKOcWNP7SKOl3TsTkBl3luS4I2UnYtdw8biviZ5NtcE1caoXPi3lVHaHs%2FjQnicwGVDlmJdvRzA%3D%3D&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&_=1471439245327
    Request Method:GET
    Status Code:500 Internal Server Error
    Remote Address:[::1]:44361


    _Response Headers_

    Cache-Control:private
    Content-Type:text/html; charset=utf-8
    Date:Wed, 17 Aug 2016 13:08:05 GMT
    Server:Microsoft-IIS/10.0
    Transfer-Encoding:chunked
    X-AspNet-Version:4.0.30319
    X-Powered-By:ASP.NET
    X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNccmFwaGFlbC5tb3JpblxTb3VyY2VcUmVwb3NcVGVhbXdvcmtTb2x1dGlvblxUZWFtd29yay5BcGlcc2lnbmFsclxzdGFydA==?=



    Here is my Startup class

    [assembly: OwinStartup(typeof(Teamwork.Api.Startup))]
    namespace Teamwork.Api
    {
     public partial class Startup
     {
      public void Configuration(IAppBuilder app)
      {
        ConfigureAuth(app);
        app.Map("/signalr", map =>
        {
          map.UseCors(CorsOptions.AllowAll);
          var hubConfiguration = new HubConfiguration {
          EnableJavaScriptProxies = false,
          EnableDetailedErrors = true};
          map.RunSignalR(hubConfiguration);
          });
        }
      }
    }



    My hub

    namespace Teamwork.Api.Hubs
    {
     public class ChatHub : Hub
     { 
      public void TransferMessage(string receiver, string message)
      {
        var name = this.Context.User.Identity.Name;
        var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
        context.Clients.Group(name).AddMessage(name, message);
        context.Clients.Group(receiver).AddMessage(receiver, message);
      }
    
      public override Task OnDisconnected(bool stopCalled)
      {
        var name = this.Context.User.Identity.Name;
        Clients.All.changeStatus(name, 4);
        return base.OnDisconnected(stopCalled);
      }
    
      public override Task OnConnected()
      {
        var name = this.Context.User.Identity.Name;
        Clients.All.changeStatus(name, 0);
        return Groups.Add(name, name);
        }
      }
    }



    I access it using an angularJS service provider that didn't have any problem until i tried to subscribe to my hub

    I tried to add this code to my Global.asax file without any success:

    Context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
    var referrer = Request.UrlReferrer;
    if (Context.Request.Path.Contains("/signalr") && referrer != null){
        Context.Response.AppendHeader("Access-Control-Allow-Origin", referrer.Scheme + ": " + referrer.Authority);
    }

    I've been looking for 4 days now for a similar issue and i can find none. As i am not proficient with webAPI and HtmlRequest, i may have miss something obvious. Any tips/ideas/answers would be greatly appreciated. If anything is missing, tell me and I'll add it as soon as possible.



    Wednesday, August 17, 2016 3:46 PM

Answers

  • User1020904144 posted

    This was due to my OnConnected() function in my hub having a null exception.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 19, 2016 3:11 PM

All replies

  • User1020904144 posted

    This was due to my OnConnected() function in my hub having a null exception.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 19, 2016 3:11 PM
  • User61956409 posted

    Hi Raph Mo,

    This was due to my OnConnected() function in my hub having a null exception.

    According to your code, we could find that you override OnConnected and OnDisconnected events, you said it throws null exception error on OnConnected event,

    var name = this.Context.User.Identity.Name;

    does  this code snippet causes the error?

    Best Regards,

    Fei Han

    Wednesday, August 31, 2016 6:11 AM
  • User1020904144 posted
    No, it was caused by returning Groups.Add(name, name);

    Instead i moved the fonction over to a call and returned the base fonction of the Hub class OnConnected().
    Wednesday, August 31, 2016 12:12 PM