Код: Выделить всё
{
public class SignalRHub : Hub
{
private readonly IEFBasketService _ebasketService;
public SignalRHub(IEFBasketService ebasketService)
{
_ebasketService = ebasketService;
}
public async Task SendBasket(int userId)
{
var basketCount = await _ebasketService.GetByUserIdBasketQuantity(userId);
await Clients.All.SendAsync("ReceiveBasketCount", basketCount);
}
}
}
Код: Выделить всё
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyHeader()
.SetIsOriginAllowed((host) => true)
.AllowCredentials()
.AllowAnyMethod();
});
});
builder.Services.AddSignalR();
app.UseCors("AllowAll");
app.MapHub("/signalrhub");
Код: Выделить всё
$(document).ready(() => {
var connection = new signalR.HubConnectionBuilder().withUrl("https://localhost:7171/SignalRHub").build();
$("#connstatus").text(connection.state);
connection.start().then(() => {
$("#connstatus").text(connection.state);
// UserId'yi HTML'den alıyoruz
var userId = $("#basketCount").data("user-id");
setInterval(() => {
connection.invoke("SendBasket", userId).catch(err => console.error(err.toString()));
}, 1000);
}).catch((err) => { console.log(err) });
connection.on("ReceiveBasketCount", (value) => {
$("#basketCount").attr("data-notify", value);
});
});
[i][/i]
При перезапуске проекта данные data-notify="2" остаются то же самое, и консоль показывает ошибку: не удалось вызвать SendBasket из-за ошибки на сервере.
Подробнее здесь: https://stackoverflow.com/questions/793 ... th-signalr
Мобильная версия