我試圖在通知JavaScript(Aurelia)客戶端(WebApp)的Web API上構建通知服務。我的Web API和WebApp位於不同的域中。無法使SignalR,Web API和SystemJS一起工作
我有一個簡單NotificationHub
:
public class NotificationHub:Hub
{
public void NotifyChange(string[] values)
{
Clients.All.broadcastChange(values);
}
}
我在網頁API的Startup
配置SignalR如下:
public void Configuration(IAppBuilder app)
{
HttpConfiguration httpConfig = new HttpConfiguration();
var cors = new EnableCorsAttribute("http://localhost:9000", "*", "*");
httpConfig.EnableCors(cors);
app.MapSignalR();
WebApiConfig.Register(httpConfig);
...
}
在我的Web應用程序,我想從我的Web訪問signalr/hubs
API如下:
Promise.all([...
System.import('jquery'),
System.import('signalr'), ...
])
.then((imports) => {
return System.import("https://localhost:44304/signalr/hubs");
});
我hav E也加入了meta
節我config.js
:
System.config({
...
meta: {
"https://*/signalr/hubs": { //here I also tried with full url
"format": "global",
"defaultExtension": false,
"defaultJSExtension": false,
"deps": ["signalr"]
}
}
});
儘管這些配置的,我還是有以下問題:
- 的請求
signalr/hubs
,從Web應用程序,作出https://localhost:44304/signalr/hubs.js
和HTTP 404
返回。請注意,瀏覽https://localhost:44304/signalr/hubs
將返回hubs
腳本。 - 在
$.connection("https://localhost:44304/signalr").start()
,我收到以下錯誤:
XMLHttpRequest cannot load https://localhost:44304/signalr/negotiate?clientProtocol=1.5&_=1471505254387 . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:9000 ' is therefore not allowed access.
請讓我知道我在這裏失蹤?
更新: 使用適當的CORS的配置,並通過@kabaehr建議gist我現在能夠連接到集線器SignalR。但是,廣播(推送通知)仍然不起作用。
我SignalR配置:
public static class SignalRConfig
{
public static void Register(IAppBuilder app, EnableCorsAttribute cors)
{
app.Map("/signalr", map =>
{
var corsOption = new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context =>
{
var policy = new CorsPolicy { AllowAnyHeader = true, AllowAnyMethod = true, SupportsCredentials = true };
// Only allow CORS requests from the trusted domains.
cors.Origins.ForEach(o => policy.Origins.Add(o));
return Task.FromResult(policy);
}
}
};
map.UseCors(corsOption).RunSignalR();
});
}
}
我使用它在Startup
如下:
var cors = new EnableCorsAttribute("http://localhost:9000", "*", "*");
httpConfig.EnableCors(cors);
SignalRConfig.Register(app, cors);
WebApiConfig.Register(httpConfig);
,我試圖以推送通知如下:
GlobalHost.ConnectionManager.GetHubContext<NotificationHub>().Clients.All.broadcastChange(new[] { value });
然而,我的客戶沒有通知broadcastChange
。我假設我不需要明確導入https://localhost:44304/signalr/hubs
,當我使用gist時。
您的來源沒有使用'https'似乎?首先嚐試用「*」來解決問題。 –
@MarkC。感謝您的評論。我曾嘗試用'「*」',但是,我真的不能說是否正常工作與否,'SystemJS'追加'在signalr/hubs'的'年底.js',和我得到一個' 404'。我認爲需要在某種程度上處理,但我不知道如何。 –
有幾個插件系統JS使用它可以預防「的.js」像https://github.com/systemjs/plugin-text的追加,但我不知道這是不是問題。 – kabaehr