2016-08-17 69 views
1

我正在爲(我自己託管的)webAPI在javascript angularJS客戶端中使用SignalR爲我們的內部應用程序開發聊天應用程序。這是跨域連接。SignalR協商成功,但在開始請求時失敗

使用SignalR 2.2.1

使用Owin 3.0.1

採用了棱角分明1.5.7如果這是相關


我的問題是,每當我試圖建立一個聯接與我中心,

[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. 

啓動請求失敗

[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. 

我已經試過將這個問題指向現在幾天了,注意到在啓動請求調用中,響應缺少'Access-Control-Allow-Origin'標頭。什麼錯誤最深的是,在談判請求,並中止請求都包含標題

協商請求

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 

響應頭

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=?= 

,但不是我的開始請求

開始請求

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 

響應頭

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==?= 

這裏是我的啓動類

[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); 
     }); 
    } 
    } 
} 

我的樞紐

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); 
     } 
    } 
} 

我使用沒有任何問題,直到我試圖訂閱我的樞紐angularJS服務提供商訪問

服務提供商

class ChatServiceProvider implements IChatServiceProvider { 
    baseUrl: string; 
    chatHub: HubProxy; 

    public setBaseUrl(url: string) { 
    this.baseUrl = url; 
    } 

    public $get(
    $rootScope: fuse.interfaces.IRootScope 
): IChatService { 
    var self = this; 
    var connection = $.hubConnection(self.baseUrl); 
    var chatHub = connection.createHubProxy("chatHub"); 
    function initialize(): JQueryPromise<any> { 
     connection.logging = true; 
     return connection.start(); 
    }; 
    return { 
     chatHub: undefined, 
     initialize:() => { 
     return initialize() 
     }, 
    on: function (eventName, callback) { 
     chatHub.on(eventName, function (result: any) { 
     $rootScope.$apply(function() { 
      if (callback) { 
      callback(result); 
      } 
     }); 
     }); 
    } 
    } 
} 

控制器

self.chatService.on("addMessage", function (name: string, message: string) { 
    this.addMessage(name, message); 
    }) 

    this.$scope.reply = function (id: string, message: string) { 
    this.chatService.chatHub.invoke("transferMessage", id, message); 
    } 

    this.chatService.initialize() 
    .done(function (data: HubProxy) { 
     self.chatService.chatHub = data; 
     console.log("Connected"); 
    }) 
    .fail(function() { console.log("Failed") }); 

我試圖將此代碼添加到我的全球。ASAX文件沒有任何成功:

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); 
} 

我一直在尋找4天現在類似的問題,我什麼也找不到。由於我不熟悉webAPI和HtmlRequest,我可能會錯過某些明顯的東西。如果沒有,那麼任何提示/想法/答案將不勝感激。如果缺少任何東西,告訴我,我會盡快添加它。

回答

相關問題