Когда я развертываю это приложение в Azure, оно по-прежнему работает нормально, но я вижу странные ошибки в консоли браузера. (только во время запуска приложения пользовательского интерфейса перед любым обменом данными):
Код: Выделить всё
[ERROR]
* The connection to https://mywebapp.eastus-01.azurewebsites.net/NotificationHub?id=aaaaaaaaaaaaaaaaaaaa-g was interrupted while the page was loading. signalr.js:2446:31
* [2024-09-21T20:15:31.733Z] Information: Connection disconnected. signalr.js:453:30
* [2024-09-21T20:15:32.420Z] Information: SSE connected to https://mywebapp.eastus-01.azurewebsites.net/NotificationHub?id=s8Tsz4ASX_0G9QDnVp9o7g signalr.js:453:30
* [2024-09-21T20:15:33.475Z] Information: SSE connected to https://mywebapp.eastus-01.azurewebsites.net/NotificationHub?id=CJxP06m_KbrlaT59XgHqtw signalr.js:453:30
[WARNING]
* Source map error: Error: request failed with status 404
Resource URL: https://myuiapp.eastus-01.azurewebsites.net/lib/microsoft/signalr/dist/browser/signalr.js
Source Map URL: signalr.js.map
[WARNING]
* Source map error: request failed with status 404
Resource URL: https://myuiapp.eastus-01.azurewebsites.net/lib/microsoft/signalr/dist/browser/signalr.js
Source Map URL: signalr.js.map
Конфигурация на стороне API (но, как я уже сказал, эта ошибка возникает во время запуска приложения до каких-либо действий по уведомлению):
Код: Выделить всё
builder.Services.AddCors(options =>
{
options.AddPolicy(name: "CorsSignalr", policyBuilder =>
policyBuilder
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.SetIsOriginAllowed(hostName => true));
});
app.UseCors("CorsSignalr");
app.MapHub("/NotificationHub");
Код: Выделить всё
var userGroupName = "name";
var group = _notificationHub.Clients.Group(userGroupName);
await group.SendAsync(
method: javascriptMethodName,
arg1: arg1,
...
userName);
Код: Выделить всё
let signalRHostName= document.getElementById("API_HOST_ADDRESS").value
var connection = new signalR.HubConnectionBuilder()
.withUrl(signalRHostName.concat("/NotificationHub"))
.withAutomaticReconnect()
.build();
Код: Выделить всё
C:\home\site\wwwroot\wwwroot\lib\microsoft\signalr\dist\browser>ls
signalr.js
signalr.min.js
ОБНОВЛЕНИЕ 1:
спасибо @LexLi, это выглядит так Хорошая идея, чтобы продолжить, но я пока не уверен, как это сделать правильно. Я изменил код в двух местах:
добавил транспорт на стороне js:
Код: Выделить всё
var connection = new signalR.HubConnectionBuilder()
.withUrl(signalRHostName.concat("/NotificationHub"), {
transport: signalR.HttpTransportType.ServerSentEvents
})
.withAutomaticReconnect()
.build();
Код: Выделить всё
app.MapHub("/NotificationHub", (options) =>
{
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.ServerSentEvents;
});
UPDATE2:
Я вызываю создание соединения в customSignalR.js сразу после нижнего колонтитула в _Layout.cshtml:
Код: Выделить всё
@await RenderSectionAsync("Scripts", required: false)
< Strong>UPDATE3:[/b]
После включения журналов отладки на стороне js во время запуска приложения я вижу:
Код: Выделить всё
The connection to https://myapi.eastus-01.azurewebsites.net/NotificationHub?id=bbbb was interrupted while the page was loading. signalr.js:2446:31
[2024-09-23T05:23:53.574Z] Debug: HttpConnection.stopConnection(undefined) called while in state Connected. signalr.js:457:30
[2024-09-23T05:23:53.575Z] Information: Connection disconnected. signalr.js:453:30
[2024-09-23T05:23:53.575Z] Debug: HubConnection.connectionClosed(undefined) called while in state Connected. signalr.js:457:30
[2024-09-23T05:23:53.575Z] Information: Connection reconnecting. signalr.js:453:30
[2024-09-23T05:23:53.575Z] Information: Reconnect attempt number 1 will start in 0 ms. signalr.js:453:30
[2024-09-23T05:23:53.575Z] Debug: Starting connection with transfer format 'Text'. signalr.js:457:30
[2024-09-23T05:23:53.575Z] Debug: Sending negotiation request: https://myapi.eastus-01.azurewebsites.net/NotificationHub/negotiate?negotiateVersion=1. signalr.js:457:30
[2024-09-23T05:23:55.002Z] Debug: Starting HubConnection. signalr.js:457:30
[2024-09-23T05:23:55.004Z] Debug: Starting connection with transfer format 'Text'. signalr.js:457:30
[2024-09-23T05:23:55.004Z] Debug: Sending negotiation request: https://myapi.eastus-01.azurewebsites.net/NotificationHub/negotiate?negotiateVersion=1. signalr.js:457:30
[2024-09-23T05:23:55.371Z] Debug: Selecting transport 'ServerSentEvents'. signalr.js:457:30
[2024-09-23T05:23:55.556Z] Information: SSE connected to https://myapi.eastus-01.azurewebsites.net/NotificationHub?id=aaaa signalr.js:453:30
[2024-09-23T05:23:55.556Z] Debug: The HttpConnection connected successfully. signalr.js:457:30
[2024-09-23T05:23:55.556Z] Debug: Sending handshake request. signalr.js:457:30
[WARNING]
Source map error: Error: request failed with status 404
Resource URL: https://myuiapp.eastus-01.azurewebsites.net/lib/microsoft/signalr/dist/browser/signalr.js
Source Map URL: signalr.js.map
[2024-09-23T05:23:56.082Z] Information: Using HubProtocol 'json'. signalr.js:453:30
[2024-09-23T05:23:56.083Z] Debug: Server handshake complete. signalr.js:457:30
[2024-09-23T05:23:56.291Z] Debug: HubConnection connected successfully
UPDATE4:
спасибо @JasonPan, очевидно, веб-сокеты не включены по умолчанию в службах приложений Azure после включив его, все ошибки исчезли, остался только один вопрос:
Код: Выделить всё
Source map error: Error: request failed with status 404
Resource URL: https://myuiapp.eastus-01.azurewebsites.net/lib/microsoft/signalr/dist/browser/signalr.js
Source Map URL: signalr.js.map
Подробнее здесь: https://stackoverflow.com/questions/790 ... -completed