#angular #asp.net-core #websocket
#угловатый #asp.net-ядро #websocket
Вопрос:
Вот журнал (консольный браузер)
[2020-11-26T11:40:56.352Z] Debug: Selecting transport 'WebSockets'.
Utils.js:228 [2020-11-26T11:40:56.353Z] Trace: (WebSockets transport) Connecting.
Utils.js:224 [2020-11-26T11:40:56.402Z] Information: WebSocket connected to ws://localhost:8080/upload-process?id=14Cj6nHlbbp1RJX6GnO0fA.
Utils.js:228 [2020-11-26T11:40:56.403Z] Debug: The HttpConnection connected successfully.
Utils.js:228 [2020-11-26T11:40:56.403Z] Debug: Sending handshake request.
Utils.js:228 [2020-11-26T11:40:56.404Z] Trace: (WebSockets transport) sending data. String data of length 39.
Utils.js:224 [2020-11-26T11:40:56.405Z] Information: Using HubProtocol 'messagepack'.
Utils.js:228 [2020-11-26T11:40:56.469Z] Trace: (WebSockets transport) socket closed.
Utils.js:228 [2020-11-26T11:40:56.469Z] Debug: HttpConnection.stopConnection(Error: WebSocket closed with status code: 1011 ().) called while in state Connected.
Utils.js:218 [2020-11-26T11:40:56.469Z] Error: Connection disconnected with error 'Error: WebSocket closed with status code: 1011 ().'.
push../node_modules/@microsoft/signalr/dist/esm/Utils.js.ConsoleLogger.log @ Utils.js:218
Utils.js:228 [2020-11-26T11:40:56.470Z] Debug: HubConnection.connectionClosed(Error: WebSocket closed with status code: 1011 ().) called while in state Connecting.
Utils.js:228 [2020-11-26T11:40:56.470Z] Debug: Hub handshake failed with error 'Error: WebSocket closed with status code: 1011 ().' during start(). Stopping HubConnection.
Utils.js:228 [2020-11-26T11:40:56.471Z] Debug: Call to HttpConnection.stop(Error: WebSocket closed with status code: 1011 ().) ignored because the connection is already in the disconnected state.
Utils.js:228 [2020-11-26T11:40:56.471Z] Debug: HubConnection failed to start successfully because of error 'Error: WebSocket closed with status code: 1011 ().'.
obs: У меня такой же журнал на JavaScript (PoC).
Angular 10
пакеты: «@microsoft/signalr»: «^ 5.0.0», «@microsoft/signalr-protocol-msgpack»: «^ 5.0.0»,
export class SignalRService extends BaseService {
private hubConnection: HubConnection;
private signalrUrl = `${this.environment.apiUpload}upload-process`;
constructor() {
super();
}
public connect = () => {
this.startConnection();
this.addListeners();
}
private getConnection(): HubConnection {
return new HubConnectionBuilder()
.withUrl(this.signalrUrl)
.withHubProtocol(new MessagePackHubProtocol())
.configureLogging(LogLevel.Trace)
.build();
}
private startConnection(): any {
this.hubConnection = this.getConnection();
this.hubConnection.start()
.then(() => console.log('connection started'))
.catch((err) => console.log('error while establishing signalr connection: ' err))
}
private addListeners(): any {
this.hubConnection.on('ProcessUpload', (data: any) => {
console.log('message received from API Controller ProcessUpload');
console.log(data);
});
}
}
API (серверная часть)
- Целевая структура: netcoreapp3.1
- Майкрософт.AspNetCore.SignalR» Версия =»1.1.0″
- Майкрософт.AspNetCore.SignalR.Протоколы.Пакет сообщений» Version=»5.0.0″
Startup.cs
Конфигурационные сервисы
services.AddCors(options => options.AddPolicy("CorsPolicy",
builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.WithOrigins("http://localhost:4200")
.SetIsOriginAllowed((host) => true)
.AllowCredentials();
}));
Настройка
app.UseCors (option => option.AllowAnyOrigin ().AllowAnyMethod ().AllowAnyHeader ().WithOrigins("http://localhost:4200").AllowCredentials());
app.UseEndpoints (endpoints => {
endpoints.MapControllers ();
endpoints.MapHub<UploadProcessHub>("/upload-process");
});
Ответ №1:
Я изменил версии de на ASP.NET ЯДРО
- Майкрософт.AspNetCore.SignalR» Версия =»1.1.0″
- Майкрософт.AspNetCore.SignalR.Протоколы.Пакет сообщений» Version=»5.0.0″
Для
- «Майкрософт.AspNetCore.SignalR.Общий»: «3.1.0.0»,
- «Microsoft.AspNetCore.SignalR.Core»: «3.1.0.0»,
- «Microsoft.AspNetCore.SignalR»: «3.1.0.0»,
- «Майкрософт.AspNetCore.SignalR.Протоколы.Json»: «3.1.0.0»,
- «Майкрософт.AspNetCore.StaticFiles»: «3.1.0.0»,
- «Майкрософт.AspNetCore.WebSockets»: «3.1.0.0»,
ЖУРНАЛ:
[2020-11-26T13:06:02.963Z] Debug: Starting HubConnection.
Utils.js:228 [2020-11-26T13:06:02.965Z] Debug: Starting connection with transfer format 'Text'.
Utils.js:228 [2020-11-26T13:06:02.967Z] Debug: Sending negotiation request: https://localhost:5001/chatHub/negotiate?negotiateVersion=1.
Utils.js:228 [2020-11-26T13:06:04.167Z] Debug: Selecting transport 'WebSockets'.
Utils.js:228 [2020-11-26T13:06:04.169Z] Trace: (WebSockets transport) Connecting.
Utils.js:224 [2020-11-26T13:06:04.231Z] Information: WebSocket connected to wss://localhost:5001/chatHub?id=d28Y5uTKpz4FYGaSqf6k6Q.
Utils.js:228 [2020-11-26T13:06:04.232Z] Debug: The HttpConnection connected successfully.
Utils.js:228 [2020-11-26T13:06:04.233Z] Debug: Sending handshake request.
Utils.js:228 [2020-11-26T13:06:04.235Z] Trace: (WebSockets transport) sending data. String data of length 32.
Utils.js:224 [2020-11-26T13:06:04.236Z] Information: Using HubProtocol 'json'.
Utils.js:228 [2020-11-26T13:06:04.283Z] Trace: (WebSockets transport) data received. String data of length 3.
Utils.js:228 [2020-11-26T13:06:04.285Z] Debug: Server handshake complete.
Utils.js:228 [2020-11-26T13:06:04.285Z] Debug: HubConnection connected successfully.
signalir.service.ts:33 connection started
Utils.js:228 [2020-11-26T13:06:12.747Z] Trace: (WebSockets transport) data received. String data of length 59.
signalir.service.ts:39 message received from API Controller ProcessUpload