【发布时间】:2019-01-15 00:58:22
【问题描述】:
我使用 Angular 和 asp.net core 2.1.3 作为后端。我添加了 SingalR 配置。如下一个包,
services.AddSignalR((hubOptions) => {
hubOptions.EnableDetailedErrors = true;
// hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
})
.AddJsonProtocol(options => {
options.PayloadSerializerSettings.ContractResolver =
new DefaultContractResolver();
}).AddHubOptions<ChatHub>(options => {
options.EnableDetailedErrors = true;
});
app.UseSignalR(routes => {
routes.MapHub<ChatHub>("/api/CRUD");
});
对于 Angular,我使用的是 "@aspnet/signalr": "^1.0.2",
我的问题在于客户端配置。连接在开始前至少持续一分钟,
我正在使用服务来启动连接,然后我订阅了组件中的值,
这是我的服务,
@Injectable()
export class SignalRService {
value = new Subject();
connectionEstablished = new Subject<Boolean>();
private hubConnection: HubConnection;
private baseUrl: string = null;
constructor(private configService: ConfigService) {
this.baseUrl = this.configService.getApiURI();
this.createConnection('CRUD');
this.startConnection();
this.registerOnServerEvents();
}
public createConnection(hubEndPoit) {
this.hubConnection = new HubConnectionBuilder()
.withUrl(this.baseUrl + hubEndPoit)
.configureLogging(signalR.LogLevel.Information)
.build();
}
private startConnection(): void {
this.hubConnection
.start()
.then(() => {
console.log('Hub connection started');
this.connectionEstablished.next(true);
})
.catch(err => {
console.log('Error while establishing connection, retrying...', err);
setTimeout(this.startConnection(), 5000);
});
}
private registerOnServerEvents(): void {
this.hubConnection.on('ReceiveMessage', (data: any) => {
this.value.next(data);
});
}
}
为什么我收到我的连接首先被规范化并且正确的连接在开始前至少持续一分钟?
更新
似乎我在某处有一个 SignalR 无法解决的 WebSocket 问题,如上面的错误中所示,wss://www.artngcore.com:4200/api/CRUD?id=0_uGI7io5iFN1SOv5Akfxw
我让它工作的唯一方法是指定传输协议,
public createConnection(hubEndPoit) {
this.hubConnection = new HubConnectionBuilder()
.withUrl(this.baseUrl + hubEndPoit, signalR.HttpTransportType.ServerSentEvents)
.configureLogging(signalR.LogLevel.Information)
.build();
}
如何解决 websocket 问题?
【问题讨论】:
-
只有在附加调试器时才会发生这种情况吗?
-
no 与调试器无关,只是通过提供服务器 url 而不是使用代理对其进行排序,我的问题是 websocket 连接
-
@davidfowl,为什么调试器会导致问题?
标签: angular signalr asp.net-core-2.1