如何解决无法连接错误:协商请求期间出错
我的代码应在angular 8 asp.net Web API 2和signalR 2之间创建聊天通信 服务器 hub
public class ChatHub : Hub
{
public string Send(string data="hello")
{
return Clients.All.SendAsync(data);
}
}
startUp
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
角客户端
我得到了错误:
协商请求时出错
解决方法
您可以尝试像这样配置它:
public void Configuration(IAppBuilder app)
{
//Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr",map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration { };
map.RunSignalR(hubConfiguration);
});
}
[HubName("ChatHub")]
public class ChatHub : Hub
{
public string Send(string data="hello")
{
return Clients.All.SendAsync(data);
}
}
//jQuery
declare var $: any;
//server configuration
export let CONFIGURATION = {
baseUrls: {
server: 'https://localhost:51962'
},}
private proxy: any;
private proxyName: string = 'ChatHub'; //defined in the hub
private configureConnection() {
// create hub connection
this.connection = $.hubConnection(CONFIGURATION.baseUrls.server);
// create new proxy as name already given in top
this.proxy = this.connection.createHubProxy(this.proxyName);
}
// check in the browser console for either signalr connected or not
private startConnection(): void {
this.connection.start().done((data: any) => {
console.log('Now connected ' + data.transport.name + ',connection ID= ' + data.id);
this.connectionEstablished.emit(true);
this.connectionExists = true;
}).fail((error: any) => {
console.log('Could not connect ' + error);
this.connectionEstablished.emit(false);
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。