gpt4 book ai didi

angular - 无法使用 Angular 5 客户端启动与信号器核心中心的连接

转载 作者:太空狗 更新时间:2023-10-29 17:47:57 25 4
gpt4 key购买 nike

我有一个运行 SignalR Core 集线器的 .Net Core 2.0 C# Web Api。我什至无法从收到此错误的 Angular (5.0.1) 启动 () 我的 hubConnection:

OPTIONS https://localhost:44301/hubs/sample 405 (Method Not Allowed)

Error: Failed to start the connection. Error: Method Not Allowed

Error while establishing connection :(

XHR failed loading: OPTIONS "https://localhost:44301/hubs/sample".

HttpError: Method Not Allowed at XMLHttpRequest.xhr.onload [as __zone_symbol__ON_PROPERTYload] (webpack-internal:///../../../../@aspnet/signalr-client/dist/src/HttpClient.js:30:28) at XMLHttpRequest.wrapFn (webpack-internal:///../../../../zone.js/dist/zone.js:1166:39) at ZoneDelegate.invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:425:31) at Object.onInvokeTask (webpack-internal:///../../../core/esm5/core.js:4816:33) at ZoneDelegate.invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:424:36) at Zone.runTask (webpack-internal:///../../../../zone.js/dist/zone.js:192:47) at ZoneTask.invokeTask [as invoke] (webpack-internal:///../../../../zone.js/dist/zone.js:499:34) at invokeTask (webpack-internal:///../../../../zone.js/dist/zone.js:1540:14) at XMLHttpRequest.globalZoneAwareCallback (webpack-internal:///../../../../zone.js/dist/zone.js:1566:17)

我什至都在努力调试这个问题,即使在 fiddler 的帮助下我也无法解决它。它似乎不是我的 Api 的问题,因为 Kestrel 控制台打印出对集线器的请求进来了,但没有打印出任何错误。而且我能够毫无问题地获取、放置和发布到我的 Controller 。

这是我的 StartUp.cs 配置(为简洁起见,我省略了不相关的代码)

public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(
"CorsPolicy",
builder =>
builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
);
});

services.AddAuthentication();
services.AddMvcCore()
.AddAuthorization(options => {...});
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options => {...});

services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("CorsPolicy");
app.UseAuthentication();

app.UseSignalR(routes =>
{
routes.MapHub<SampleHub>("/hubs/sample");
});

app.UseMvc();
}

这是我的示例中心

public interface ISampleHubClient 
{
Task receiveMessage(SampleRequest msg);
}

public class SampleHub : Hub<ISampleHubClient>
{
private static string _connectionId;

public async Task Subscribe(string groupName)
{
await Groups.AddAsync(Context.ConnectionId, groupName);

await Clients.Client(Context.ConnectionId).receiveMessage("Subscribed to " + groupName);
}

/// <inheritdoc />
public Task Send(SampleRequest msg)
{
ConsoleColor.Green.WriteLine("Send : " + _connectionId);

return Clients.Group(msg.Group).receiveMessage(msg);
}
}

这是我的 Angular 组件代码:

import { HubConnection } from '@aspnet/signalr-client';

@Component({})
export class SignalRTestComponent implements OnInit {
private hubConnection: HubConnection;

ngOnInit() {
this.initHub();
}

initHub()
{
let self = this;
var signalrBaseUri = "https://localhost:44301/hubs/sample";

this.hubConnection = new HubConnection(signalrBaseUri);

this.hubConnection
.start() // <- Failure here
.then(() =>{
self.hubConnection.invoke("Subscribe", this.groupName);
})
.catch(err => { console.log("INVOKE FAILED");console.error(err);});
})
.catch(err =>
{
console.log('Error while establishing connection :(');
console.log(err);
});

this.hubConnection.on('receiveMessage', (receivedMessage: SampleMessage) => {
console.log("Message received : ");
console.log(sampleMessage);
});
}
}

任何帮助将不胜感激

最佳答案

问题是,the npm package has been renamed@aspnet/signalr-client@aspnet/signalr。更改为这个新包并修复导入对我有用(至少摆脱 OPTIONS 调用错误。

从“@aspnet/signalr”导入 { HubConnection}

关于angular - 无法使用 Angular 5 客户端启动与信号器核心中心的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48121984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com